Find the last positive element remaining after repeated subtractions of smallest positive element from all Array elements
Last Updated :
19 Apr, 2023
Given an array arr[] consisting of N positive integers, the task is to find the last positive array element remaining after repeated subtractions of the smallest positive array element from all array elements.
Examples:
Input: arr[] = {3, 5, 4, 7}
Output: 2
Explanation:
Subtract the smallest positive element from the array, i.e. 3, the new array is arr[] = {0, 2, 1, 4}
Subtract the smallest positive element from the array, i.e. 1, the new array is arr[] = {0, 1, 0, 3}
Subtract the smallest positive element from the array, i.e. 1, the new array is arr[] = {0, 0, 0, 2}
The last remaining element is 2.
Input: arr[] = {2, 6, 7}
Output: 1
Naive Approach: The simplest approach to solve the given problem is to traverse the given array arr[] and find the smallest positive element in the array and subtract it from all the elements. Perform this operation until there is only one positive element left in the array. After completing the above steps, print the remaining positive array element.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by observing that the last positive element will be the difference between the largest array element and the second-largest array element. Follow the steps below to solve the problem:
- If N = 1, print the first element of the array.
- Otherwise print the difference between the largest and second-largest array elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int lastPositiveElement(vector< int > arr)
{
int N = arr.size();
if (N == 1)
return arr[0];
int greatest = -1, secondGreatest = -1;
for ( int x : arr) {
if (x >= greatest) {
secondGreatest = greatest;
greatest = x;
}
else if (x >= secondGreatest) {
secondGreatest = x;
}
}
return greatest - secondGreatest;
}
int main()
{
vector< int > arr = { 3, 5, 4, 7 };
cout << lastPositiveElement(arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int lastPositiveElement( int [] arr)
{
int N = arr.length;
if (N == 1 )
return arr[ 0 ];
int greatest = - 1 , secondGreatest = - 1 ;
for ( int x : arr) {
if (x >= greatest) {
secondGreatest = greatest;
greatest = x;
}
else if (x >= secondGreatest) {
secondGreatest = x;
}
}
return greatest - secondGreatest;
}
public static void main(String[] args){
int [] arr = { 3 , 5 , 4 , 7 };
System.out.print(lastPositiveElement(arr));
}
}
|
Python3
def lastPositiveElement(arr) :
N = len (arr);
if (N = = 1 ) :
return arr[ 0 ];
greatest = - 1 ; secondGreatest = - 1 ;
for x in arr :
if (x > = greatest) :
secondGreatest = greatest;
greatest = x;
elif (x > = secondGreatest) :
secondGreatest = x;
return greatest - secondGreatest;
if __name__ = = "__main__" :
arr = [ 3 , 5 , 4 , 7 ];
print (lastPositiveElement(arr));
|
C#
using System;
class GFG {
static int lastPositiveElement( int [] arr)
{
int N = arr.Length;
if (N == 1)
return arr[0];
int greatest = -1, secondGreatest = -1;
for ( int x = 0; x < N; x++) {
if (arr[x] >= greatest) {
secondGreatest = greatest;
greatest = arr[x];
}
else if (arr[x] >= secondGreatest) {
secondGreatest = arr[x];
}
}
return greatest - secondGreatest;
}
public static void Main()
{
int [] arr = { 3, 5, 4, 7 };
Console.Write(lastPositiveElement(arr));
}
}
|
Javascript
<script>
function lastPositiveElement(arr) {
let N = arr.length;
if (N == 1)
return arr[0];
let greatest = -1, secondGreatest = -1;
for (let x of arr) {
if (x >= greatest) {
secondGreatest = greatest;
greatest = x;
}
else if (x >= secondGreatest) {
secondGreatest = x;
}
}
return greatest - secondGreatest;
}
let arr = [3, 5, 4, 7];
document.write(lastPositiveElement(arr));
</script>
|
Time Complexity: O(N), The time complexity of the given program is O(n), where n is the size of the input array. This is because the program traverses the input array once, performing constant time operations at each iteration.
Auxiliary Space: O(1), The space complexity of the given program is O(1), which is constant. This is because the program uses only a fixed amount of memory to store variables, regardless of the size of the input array. Specifically, the program uses three integer variables to store the greatest, second greatest, and final answer, respectively.
Share your thoughts in the comments
Please Login to comment...