Find the last positive element remaining after repeated subtractions of smallest positive element from all Array elements
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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate last positive // element of the array int lastPositiveElement(vector< int > arr) { int N = arr.size(); // Return the first element if N = 1 if (N == 1) return arr[0]; // Stores the greatest and the second // greatest element int greatest = -1, secondGreatest = -1; // Traverse the array A[] for ( int x : arr) { // If current element is greater // than the greatest element if (x >= greatest) { secondGreatest = greatest; greatest = x; } // If current element is greater // than second greatest element else if (x >= secondGreatest) { secondGreatest = x; } } // Return the final answer return greatest - secondGreatest; } // Driver Code int main() { vector< int > arr = { 3, 5, 4, 7 }; cout << lastPositiveElement(arr); return 0; } |
Java
// JAVA program for the above approach import java.util.*; class GFG{ // Function to calculate last positive // element of the array static int lastPositiveElement( int [] arr) { int N = arr.length; // Return the first element if N = 1 if (N == 1 ) return arr[ 0 ]; // Stores the greatest and the second // greatest element int greatest = - 1 , secondGreatest = - 1 ; // Traverse the array A[] for ( int x : arr) { // If current element is greater // than the greatest element if (x >= greatest) { secondGreatest = greatest; greatest = x; } // If current element is greater // than second greatest element else if (x >= secondGreatest) { secondGreatest = x; } } // Return the final answer return greatest - secondGreatest; } // Driver Code public static void main(String[] args){ int [] arr = { 3 , 5 , 4 , 7 }; System.out.print(lastPositiveElement(arr)); } } // This code is contributed by sanjoy_62. |
Python3
# Python3 program for the above approach # Function to calculate last positive # element of the array def lastPositiveElement(arr) : N = len (arr); # Return the first element if N = 1 if (N = = 1 ) : return arr[ 0 ]; # Stores the greatest and the second # greatest element greatest = - 1 ; secondGreatest = - 1 ; # Traverse the array A[] for x in arr : # If current element is greater # than the greatest element if (x > = greatest) : secondGreatest = greatest; greatest = x; # If current element is greater # than second greatest element elif (x > = secondGreatest) : secondGreatest = x; # Return the final answer return greatest - secondGreatest; # Driver Code if __name__ = = "__main__" : arr = [ 3 , 5 , 4 , 7 ]; print (lastPositiveElement(arr)); # This code is contributed by AnkThon |
C#
// C# program for the above approach using System; class GFG { // Function to calculate last positive // element of the array static int lastPositiveElement( int [] arr) { int N = arr.Length; // Return the first element if N = 1 if (N == 1) return arr[0]; // Stores the greatest and the second // greatest element int greatest = -1, secondGreatest = -1; // Traverse the array A[] for ( int x = 0; x < N; x++) { // If current element is greater // than the greatest element if (arr[x] >= greatest) { secondGreatest = greatest; greatest = arr[x]; } // If current element is greater // than second greatest element else if (arr[x] >= secondGreatest) { secondGreatest = arr[x]; } } // Return the final answer return greatest - secondGreatest; } // Driver Code public static void Main() { int [] arr = { 3, 5, 4, 7 }; Console.Write(lastPositiveElement(arr)); } } // This code is contributed by subhammahato348. |
Javascript
<script> // JavaScript Program to implement // the above approach // Function to calculate last positive // element of the array function lastPositiveElement(arr) { let N = arr.length; // Return the first element if N = 1 if (N == 1) return arr[0]; // Stores the greatest and the second // greatest element let greatest = -1, secondGreatest = -1; // Traverse the array A[] for (let x of arr) { // If current element is greater // than the greatest element if (x >= greatest) { secondGreatest = greatest; greatest = x; } // If current element is greater // than second greatest element else if (x >= secondGreatest) { secondGreatest = x; } } // Return the final answer return greatest - secondGreatest; } // Driver Code let arr = [3, 5, 4, 7]; document.write(lastPositiveElement(arr)); // This code is contributed by Potta Lokesh </script> |
2
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.
Please Login to comment...