Given an array arr[] of N elements, the task is to perform the following operation:
- Pick the two largest element from the array and remove these element. If the elements are unequal then insert the absolute difference of the elements into the array.
- Perform the above operations untill array has 1 or no element in it. If the array has only one element left then print that element, else print “-1”.
Examples:
Input: arr[] = { 3, 5, 2, 7 }
Output: 1
Explanation:
The two largest elements are 7 and 5. Discard them. Since both are not equal, insert 7 – 5 = 2 into the array. Hence, arr[] = { 3, 2, 2 }
The two largest elements are 3 and 2. Discard them. Since both are not equal, insert 3 – 2 = 1 into the array. Hence, arr[] = { 1, 2 }
The two largest elements are 2 and 1. Discard them. Since both are not equal, insert 2 – 1 = 1 into the array. Hence, arr[] = { 1 }
The only element left is 1. Print the value of the only element left.Input: arr[] = { 3, 3 }
Output: -1
Explanation:
The two largest elements are 3 and 3. Discard them. Now the array has no elements. So, print -1.
Approach: To solve the above problem we will use Priority Queue Data Structure. Below are the steps:
- Insert all the array elements in the Priority Queue.
- As priority queue is based on the implementation of Max-Heap. Therefore removing element from it gives the maximum element.
- Till the size of priority queue is not less than 2, remove two elements(say X & Y) from it and do the following:
- If X and Y are not same then insert the absolute value of X and Y into the priority queue.
- Else return to step 3.
- If the heap has only one element then print that element.
- Else print “-1”.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to print the remaining element int final_element(int arr[], int n) { // Priority queue can be used // to construct max-heap priority_queue<int> heap; // Insert all element of arr[] // into priority queue for (int i = 0; i < n; i++) heap.push(arr[i]); // Perform operation until heap // size becomes 0 or 1 while (heap.size() > 1) { // Remove largest element int X = heap.top(); heap.pop(); // Remove 2nd largest element int Y = heap.top(); heap.pop(); // If extracted element // are not equal if (X != Y) { // Find X - Y and push // it to heap int diff = abs(X - Y); heap.push(diff); } } // If heap size is 1, then // print the remaining element if (heap.size() == 1) { cout << heap.top(); } // Else print "-1" else { cout << "-1"; } } // Driver Code int main() { // Given array arr[] int arr[] = { 3, 5, 2, 7 }; // Size of array arr[] int n = sizeof(arr) / sizeof(arr[0]); // Function Call final_element(arr, n); return 0; } |
Java
// Java program for the above approach import java.io.*; import java.util.Collections; import java.util.*; class GFG{ // Function to print the remaining element static public int final_element(Integer[] arr, int n) { if(arr == null) { return 0; } // Priority queue can be used // to construct max-heap PriorityQueue<Integer> heap = new PriorityQueue<Integer>(Collections.reverseOrder()); // Insert all element of arr[] // into priority queue for(int i = 0; i < n; i++) { heap.offer(arr[i]); } // Perform operation until heap // size becomes 0 or 1 while (heap.size() > 1) { // Remove largest element int X = heap.poll(); // Remove 2nd largest element int Y = heap.poll(); // If extracted element // are not equal if (X != Y) { // Find X - Y and push // it to heap int diff = Math.abs(X - Y); heap.offer(diff); } } // If heap size is 1, then // print the remaining element // Else print "-1" return heap.size() == 1 ? heap.poll() : -1; } // Driver code public static void main (String[] args) { // Given array arr[] Integer arr[] = new Integer[] { 3, 5, 2, 7}; // Size of array arr[] int n = arr.length; // Function Call System.out.println(final_element(arr, n)); } } // This code is contributed by deepika_sharma |
1
Time Complexity: O(N*log(N))
Auxillary Space Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Replace elements with absolute difference of smallest element on left and largest element on right
- Array value by repeatedly replacing max 2 elements with their absolute difference
- Average of remaining elements after removing K largest and K smallest elements from array
- Find the largest after deleting the given elements
- Find the k largest numbers after deleting the given elements
- Replacing an element makes array elements consecutive
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Element equal to the sum of all the remaining elements
- Maximum sum of Array formed by replacing each element with sum of adjacent elements
- Maximum possible middle element of the array after deleting exactly k elements
- Check if the array has an element which is equal to XOR of remaining elements
- Check if the array has an element which is equal to sum of all the remaining elements
- Check if the array has an element which is equal to product of remaining elements
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Array formed using sum of absolute differences of that element with all other elements
- Sum of minimum absolute difference of each array element
- Longest subarray in which absolute difference between any two element is not greater than X
- k-th smallest absolute difference of two elements in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : deepika_sharma

