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 until 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++
#include <bits/stdc++.h>
using namespace std;
int final_element( int arr[], int n)
{
priority_queue< int > heap;
for ( int i = 0; i < n; i++)
heap.push(arr[i]);
while (heap.size() > 1) {
int X = heap.top();
heap.pop();
int Y = heap.top();
heap.pop();
if (X != Y) {
int diff = abs (X - Y);
heap.push(diff);
}
}
if (heap.size() == 1) {
cout << heap.top();
}
else {
cout << "-1" ;
}
}
int main()
{
int arr[] = { 3, 5, 2, 7 };
int n = sizeof (arr) / sizeof (arr[0]);
final_element(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.Collections;
import java.util.*;
class GFG{
static public int final_element(Integer[] arr, int n)
{
if (arr == null )
{
return 0 ;
}
PriorityQueue<Integer> heap = new
PriorityQueue<Integer>(Collections.reverseOrder());
for ( int i = 0 ; i < n; i++)
{
heap.offer(arr[i]);
}
while (heap.size() > 1 )
{
int X = heap.poll();
int Y = heap.poll();
if (X != Y)
{
int diff = Math.abs(X - Y);
heap.offer(diff);
}
}
return heap.size() == 1 ? heap.poll() : - 1 ;
}
public static void main (String[] args)
{
Integer arr[] = new Integer[] { 3 , 5 , 2 , 7 };
int n = arr.length;
System.out.println(final_element(arr, n));
}
}
|
Python3
from queue import PriorityQueue
def final_element(arr, n):
heap = PriorityQueue()
for i in range (n):
heap.put( - 1 * arr[i])
while (heap.qsize() > 1 ):
X = - 1 * heap.get()
Y = - 1 * heap.get()
if (X ! = Y):
diff = abs (X - Y)
heap.put( - 1 * diff)
if (heap.qsize() = = 1 ):
print ( - 1 * heap.get())
else :
print ( "-1" )
if __name__ = = '__main__' :
arr = [ 3 , 5 , 2 , 7 ]
n = len (arr)
final_element(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void final_element( int [] arr, int n)
{
List< int > heap = new List< int >();
for ( int i = 0; i < n; i++)
heap.Add(arr[i]);
while (heap.Count > 1)
{
heap.Sort();
heap.Reverse();
int X = heap[0];
heap.RemoveAt(0);
int Y = heap[0];
heap.RemoveAt(0);
if (X != Y)
{
int diff = Math.Abs(X - Y);
heap.Add(diff);
}
}
if (heap.Count == 1)
{
heap.Sort();
heap.Reverse();
Console.Write(heap[0]);
}
else
{
Console.Write(-1);
}
}
static void Main()
{
int [] arr = { 3, 5, 2, 7 };
int n = arr.Length;
final_element(arr, n);
}
}
|
Javascript
<script>
function final_element(arr, n)
{
var heap = [];
for ( var i = 0; i < n; i++)
heap.push(arr[i]);
heap.sort((a,b)=>a-b);
while (heap.length > 1) {
var X = heap[heap.length-1];
heap.pop();
var Y = heap[heap.length-1];
heap.pop();
if (X != Y) {
var diff = Math.abs(X - Y);
heap.push(diff);
}
heap.sort((a,b)=>a-b);
}
if (heap.length == 1) {
document.write(heap[heap.length-1]);
}
else {
document.write( "-1" );
}
}
var arr = [3, 5, 2, 7 ];
var n = arr.length;
final_element(arr, n);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space Complexity: O(N)