VOOZH about

URL: https://www.geeksforgeeks.org/dsa/reduce-array-by-replacing-pair-with-their-difference/

⇱ Reduce Array by replacing pair with their difference - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Reduce Array by replacing pair with their difference

Last Updated : 20 Dec, 2023

You are given an array of positive integers. You can perform the following operations on the array any number of times:

  • Select the two largest elements in the array, suppose a and b.
  • If a = b, remove both of them from the array.
  • If a > b, remove b and the new value of a will be a-b or vice-versa.

This process continues until there is either one element left or all the elements have been removed. You have to find the last element remaining or return 0 if there is none.

Examples:

Input: [1, 2, 3, 6, 7, 7]
Output: 0
Explanation: We start with two largest elements. In this case, they are two equal elements with value 7, so they both are removed. Then, the next elements are 3 and 6. Discard the smaller one and the larger one will now be of length 6-3 = 3. Then, the next elements are of 3 and 2. Again the smaller will be discarded and larger one will now be 3-2 = 1. Now there are two equal elements, 1, hence both will be discarded and output will be 0.

Input: [2, 4, 5]
Output: 1
Explanation: In this case, the two largest elements are 4 and 5. As 4 is smaller it will be discarded and the larger will become 5-4 = 1. Now there are only two elements remaining which are 1 and 2. Hence 1 will be discarded and the final output will be 2-1 = 1.

Approach:

As the problem has to repeatedly find the two largest items from a given array, we can think of a priority queue (Max heap) in this case. Using a max heap, we can perform the required operations until the given condition is reached.

Follow the below steps to implement the above idea:

  • Create a priority queue (max heap) named 'pq' to store the items in a decreasingly sorted manner.
  • Traverse through the array and insert each item into the heap.
  • Till there are atleast 2 items in the heap, perform the following operations as stated in the problem:
    • Store the two largest elements from the max-heap in two different variables named as 'a' and 'b'.
    • If the values of 'a' and 'b' are not equal, reduce the length of 'b' from 'a'.
    • Push the updated value of 'a' into the max-heap and continue performing these steps.
  • Once these above operations are performed, check if the size of pq is 1.
  • If it is equal to 1, that means one element is remaining which cannot be integrated further. So return the element as our answer.
  • If it is not 1, then all the elements have been removed and none is remaining. So we return 0 as our answer.

Implementation of the above approach:


Output
1

Time Complexity: O(N*log(N)). The all insertions in a priority queue takes O(N*log(N)) time and the time for each pop and top operation takes O(log(N)) time.
Auxiliary space: O(N),as we have used an extra space of a priority queue data structure.

Comment