Reduce the array to atmost one element by the given operations
Last Updated :
27 Jan, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int reduceArray(vector< int >& arr)
{
priority_queue< int > p;
for ( int i = 0; i < arr.size(); ++i)
p.push(arr[i]);
while (p.size() > 1) {
int y = p.top();
p.pop();
int x = p.top();
p.pop();
int val = y - x;
if (val != 0)
p.push(val);
}
if (p.size() == 0)
return 0;
return p.top();
}
int main()
{
vector< int > arr
= { 3, 4, 6, 2, 7, 1 };
cout << reduceArray(arr)
<< "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int reduceArray( int [] arr)
{
PriorityQueue<Integer> p = new PriorityQueue<>(
11 , Collections.reverseOrder());
for ( int i = 0 ; i < arr.length; ++i)
p.add(arr[i]);
while (p.size() > 1 )
{
int y = p.poll();
int x = p.poll();
int val = y - x;
if (val != 0 )
p.add(val);
}
if (p.size() == 0 )
return 0 ;
return p.poll();
}
public static void main(String[] args)
{
int arr[] = { 3 , 4 , 6 , 2 , 7 , 1 };
System.out.println(reduceArray(arr));
}
}
|
Python3
import heapq
def reduceArray(arr):
heapq._heapify_max(arr)
while len (arr) > 1 :
x = arr[ 0 ]
arr[ 0 ] = arr[ - 1 ]
arr = arr[: - 1 ]
heapq._heapify_max(arr)
y = arr[ 0 ]
arr[ 0 ] = arr[ - 1 ]
arr = arr[: - 1 ]
val = abs (x - y)
if val ! = 0 :
arr.append(val)
heapq._heapify_max(arr)
if len (arr) = = 0 :
return 0
return arr[ 0 ]
arr = [ 3 , 4 , 6 , 2 , 7 , 1 ]
print (reduceArray(arr))
'''Code is contributed by Rajat Kumar (GLAU)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
namespace reduce_array
{
class Program
{
static int reduceArray(List< int > arr)
{
SortedSet< int > p = new SortedSet< int >();
foreach ( int i in arr)
{
p.Add(i);
}
while (p.Count > 1)
{
int y = p.Max;
p.Remove(y);
int x = p.Max;
p.Remove(x);
int val = y - x;
if (val != 0)
p.Add(val);
}
if (p.Count == 0)
return 0;
return p.Max;
}
static void Main( string [] args)
{
List< int > arr
= new List< int > {3, 4, 6, 2, 7, 1 };
Console.WriteLine(reduceArray(arr));
}
}
}
|
Javascript
<script>
function reduceArray(arr){
let p = []
for (let i = 0; i < arr.length; i++)
p.push(arr[i])
p.sort().reverse()
while (p.length > 1){
let y = p[0]
p.splice(0,1)
let x = p[0]
p.splice(0,1)
let val = y - x
if (val != 0)
p.push(val)
p.sort().reverse()
}
if (p.length == 0)
return 0
return p[0]
}
let arr = [ 3, 4, 6, 2, 7, 1 ]
document.write(reduceArray(arr), "</br>" )
</script>
|
Time Complexity: O(N*logN)
We first build a priority queue by iterating over the array which takes O(N) time. Then, we iterate over the priority queue to find the remaining element which takes O(N*logN) time. Thus, the overall time complexity of the algorithm is O(N*logN).
Space Complexity: O(N)
We are using a priority queue to store all the elements of the array. Thus, the space complexity of the algorithm is O(N).
Share your thoughts in the comments
Please Login to comment...