Given an array arr size N, the task is to print the final array value remaining in the array when the maximum and second maximum element of the array is replaced by their absolute difference in the array, repeatedly.
Note: If the maximum two elements are same, then both are removed from the array, without replacing any value.
Input: arr = [2, 7, 4, 1, 8, 1]
Merging 7 and 8: absolute difference = 7 – 8 = 1. So the array converted into [2, 4, 1, 1, 1].
Merging 2 and 4: absolute difference = 4 – 2 = 2. So the array converted into [2, 1, 1, 1].
Merging 2 and 1: absolute difference = 2 – 1 = 1. So the array converted into [1, 1, 1].
Merging 1 and 1: absolute difference = 4 – 2 = 0. So nothing will be Merged.
So final array = .
Input: arr = [7, 10, 5, 4, 11, 25]
Efficient Approach: Using Priority Queue
- Make a priority queue(binary max heap) which automatically arrange the element in sorted order.
- Then pick the first element (which is maximum) and 2nd element(2nd max), if both are equal then don’t push anything, if not equal push absolute difference of both in queue.
- Do the above steps till queue size is equal to 1, then return last element. If queue becomes empty before reaching size 1, then return 0.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxillary Complexity: O(N)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Absolute Difference of even and odd indexed elements in an Array
- k-th smallest absolute difference of two elements in an array
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Absolute Difference of all pairwise consecutive elements in an array
- Maximum absolute difference between distinct elements in an Array
- Count maximum elements of an array whose absolute difference does not exceed K
- Find K elements whose absolute difference with median of array is maximum
- Sort an Array based on the absolute difference of adjacent elements
- Minimum absolute difference of adjacent elements in a circular array
- Sort array such that absolute difference of adjacent elements is in increasing order
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Minimize the sum calculated by repeatedly removing any two elements and inserting their sum to the Array
- Largest number in given Array formed by repeatedly combining two same elements
- Game of replacing array elements
- Replacing an element makes array elements consecutive
- Maximum sum of Array formed by replacing each element with sum of adjacent elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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 : ishayadav181