Given an array of integers **arr[]**, the task is to find the remaining element in the array after performing the following operations:

- In each turn, choose the two maximum integers X and Y from the array.
- If X == Y, remove both elements from the array.
- If X != Y, insert an element into the array equal to
**abs(X – Y)**.

**Note:** If no element remains, print 0.

**Examples:**

Input:arr[] = [3, 4, 6, 2, 7, 1]

Output:1

Explanation:

Elements 7 and 6 are reduced to 1 so the array converts to [3, 4, 2, 1, 1]

Elements 3 and 4 are reduced to 2 so the array converts to [2, 1, 1, 1]

Elements 2 and 1 are reduced to 1 so the array converts to [1, 1, 1]

Element 1 is completely destroyed by another 1 so array is [1] at the end.

Input:arr[] = [1, 2, 3, 4, 5]

Output:1

Explanation:

Elements 4 and 5 are reduced to 1 so the array converts to [1, 2, 3, 1]

Elements 2 and 3 are reduced to 1 so the array converts to [1, 1, 1]

Element 1 is completely destroyed by another 1 so array is [1] at the end.

**Approach:**

To solve the problem mentioned above, we need to use Heap Data Structure. Heaps are used because we only require the current maximum element for every instant and are not concerned about the rest of the elements.

- Create a Max Heap from the elements of the given array.
- Keep extracting the
**top**element twice for every iteration. If their absolute difference is non-zero,**push**their difference back to the queue. - Continue until only one or no elements are remaining.
- If no elements remain, print 0. Otherwise, print the remaining element.

Below is the implementation of above approach:

## C++

`// C++ Program to reduce the ` `// array to almost one element ` `// by the given operations ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the remaining ` `// element of array ` `int` `reduceArray(vector<` `int` `>& arr) ` `{ ` ` ` `priority_queue<` `int` `> p; ` ` ` ` ` `// Build a priority queue ` ` ` `// using the array ` ` ` `for` `(` `int` `i = 0; i < arr.size(); ++i) ` ` ` `p.push(arr[i]); ` ` ` ` ` `// Continue until the priority ` ` ` `// queue has one or no elements ` ` ` `// remaining ` ` ` `while` `(p.size() > 1) { ` ` ` ` ` `// Top-most integer from heap ` ` ` `int` `y = p.top(); ` ` ` `p.pop(); ` ` ` ` ` `// Second integer from heap ` ` ` `int` `x = p.top(); ` ` ` `p.pop(); ` ` ` ` ` `// Resultant value ` ` ` `int` `val = y - x; ` ` ` `if` `(val != 0) ` ` ` `p.push(val); ` ` ` `} ` ` ` ` ` `// Return 0 if no elements are left ` ` ` `if` `(p.size() == 0) ` ` ` `return` `0; ` ` ` ` ` `// Return top value of the heap ` ` ` `return` `p.top(); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `vector<` `int` `> arr ` ` ` `= { 3, 4, 6, 2, 7, 1 }; ` ` ` `cout << reduceArray(arr) ` ` ` `<< ` `"\n"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to reduce the ` `# array to almost one element ` `# by the given operations ` ` ` `# Function to find the remaining ` `# element of array ` `def` `reduceArray(arr): ` ` ` ` ` `p ` `=` `[] ` ` ` ` ` `# Build a priority queue ` ` ` `# using the array ` ` ` `for` `i ` `in` `range` `(` `len` `(arr)): ` ` ` `p.append(arr[i]) ` ` ` `p.sort(reverse ` `=` `True` `) ` ` ` ` ` `# Continue until the priority ` ` ` `# queue has one or no elements ` ` ` `# remaining ` ` ` `while` `(` `len` `(p) > ` `1` `): ` ` ` ` ` `# Top-most integer from heap ` ` ` `y ` `=` `p[` `0` `] ` ` ` `p ` `=` `p[` `1` `:] ` ` ` ` ` `# Second integer from heap ` ` ` `x ` `=` `p[` `0` `] ` ` ` `p ` `=` `p[` `1` `:] ` ` ` ` ` `# Resultant value ` ` ` `val ` `=` `y ` `-` `x ` ` ` ` ` `if` `(val !` `=` `0` `): ` ` ` `p.append(val) ` ` ` `p.sort(reverse ` `=` `True` `) ` ` ` ` ` `# Return 0 if no elements are left ` ` ` `if` `(` `len` `(p) ` `=` `=` `0` `): ` ` ` `return` `0` ` ` ` ` `# Return top value of the heap ` ` ` `return` `p[` `0` `] ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `arr ` `=` `[ ` `3` `, ` `4` `, ` `6` `, ` `2` `, ` `7` `, ` `1` `] ` ` ` ` ` `print` `(reduceArray(arr)) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time 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:

- Minimum number of given operations required to reduce the array to 0 element
- Check whether an array can be made strictly increasing by modifying atmost one element
- Reduce every element of the array to it's half retaining the sum zero
- Reduce the array such that each element appears at most 2 times
- Reduce the array such that each element appears at most K times
- Reduce a given Binary Array to a single element by removal of Triplets
- Minimum element left from the array after performing given operations
- Find maximum value of the last element after reducing the array with given operations
- Count the number of pop operations on stack to get each element of the array
- Minimum pair sum operations to make array each element divisible by 4
- Count of operations to make all elements of array a[] equal to its min element by performing a[i] – b[i]
- Minimize operations required to make each element of Array equal to it's index value
- Min and max length subarray having adjacent element difference atmost K
- Maximize the maximum subarray sum after removing atmost one element
- Count the number of operations required to reduce the given number
- Find Array formed by adding each element of given array with largest element in new array to its left
- Reduce the array to a single integer with the given operation
- Minimum steps required to reduce all the elements of the array to zero
- Largest element smaller than current element on left for every element in Array
- Form an array of distinct elements with each element as sum of an element from each 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.