Array value by repeatedly replacing max 2 elements with their absolute difference
Last Updated :
27 Sep, 2022
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.
Examples:
Input: arr = [2, 7, 4, 1, 8, 1]
Output: 1
Explanations:
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 = 1-1 = 0. So nothing will be Merged.
So final array = [1].
Input: arr = [7, 10, 5, 4, 11, 25]
Output: 2
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:
C++
#include <bits/stdc++.h>
using namespace std;
int lastElement(vector< int >& arr)
{
priority_queue< int > pq;
for ( int i = 0; i < arr.size(); i++) {
pq.push(arr[i]);
}
int m1, m2;
while (!pq.empty()) {
if (pq.size() == 1)
return pq.top();
m1 = pq.top();
pq.pop();
m2 = pq.top();
pq.pop();
if (m1 != m2)
pq.push(m1 - m2);
}
return 0;
}
int main()
{
vector< int > arr = { 2, 7, 4, 1, 8, 1, 1 };
cout << lastElement(arr) << endl;
return 0;
}
|
C
#include <bits/stdc++.h>
using namespace std;
int lastElement(vector< int >& arr)
{
priority_queue< int > pq;
for ( int i = 0; i < arr.size(); i++) {
pq.push(arr[i]);
}
int m1, m2;
while (!pq.empty()) {
if (pq.size() == 1)
return pq.top();
m1 = pq.top();
pq.pop();
m2 = pq.top();
pq.pop();
if (m1 != m2)
pq.push(m1 - m2);
}
return 0;
}
int main()
{
vector< int > arr = { 2, 7, 4, 1, 8, 1, 1 };
cout << lastElement(arr) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int lastElement( int [] arr)
{
PriorityQueue<Integer> pq = new PriorityQueue<>(
(a, b) -> b - a);
for ( int i = 0 ; i < arr.length; i++)
pq.add(arr[i]);
int m1, m2;
while (!pq.isEmpty())
{
if (pq.size() == 1 )
{
return pq.poll();
}
m1 = pq.poll();
m2 = pq.poll();
if (m1 != m2)
pq.add(m1 - m2);
}
return 0 ;
}
public static void main(String[] args)
{
int [] arr = new int []{ 2 , 7 , 4 , 1 , 8 , 1 , 1 };
System.out.println(lastElement(arr));
}
}
|
Python3
from queue import PriorityQueue
def lastElement(arr):
pq = PriorityQueue()
for i in range ( len (arr)):
pq.put( - 1 * arr[i])
m1 = 0
m2 = 0
while not pq.empty():
if pq.qsize() = = 1 :
return - 1 * pq.get()
else :
m1 = - 1 * pq.get()
m2 = - 1 * pq.get()
if m1 ! = m2 :
pq.put( - 1 * abs (m1 - m2))
return 0
arr = [ 2 , 7 , 4 , 1 , 8 , 1 , 1 ]
print (lastElement(arr))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int lastElement( int [] arr)
{
Queue< int > pq = new Queue< int >();
for ( int i = 0; i < arr.Length; i++)
pq.Enqueue(arr[i]);
int m1, m2;
while (pq.Contains(0))
{
if (pq.Count == 1)
{
return pq.Peek();
}
m1 = pq.Dequeue();
m2 = pq.Peek();
if (m1 != m2)
pq.Enqueue(m1 - m2);
}
return 0;
}
public static void Main(String[] args)
{
int [] arr = { 2, 7, 4, 1, 8, 1, 1 };
Console.WriteLine(lastElement(arr));
}
}
|
Javascript
<script>
function lastElement(arr) {
let pq = [];
for (let i = 0; i < arr.length; i++) {
pq.push(arr[i]);
}
let m1, m2;
while (pq.length) {
if (pq.length == 1)
return pq[pq.length - 1];
pq.sort((a, b) => a - b)
m1 = pq[pq.length - 1];
pq.pop();
m2 = pq[pq.length - 1];
pq.pop();
if (m1 != m2)
pq.push(m1 - m2);
}
return 0;
}
let arr = [2, 7, 4, 1, 8, 1, 1];
document.write(lastElement(arr) + "<br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Complexity: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...