Minimize remaining array element by removing pairs and replacing them with their average
Given an array arr[] of size N, the task is to find the smallest possible remaining array element by repeatedly removing a pair, say (arr[i], arr[j]) from the array and inserting the Ceil value of their average.
Examples:
Input: arr[] = { 1, 2, 3 }
Output: 2
Explanation:
Removing the pair (arr[1], arr[2]) from arr[] and inserting (arr[1] + arr[2] + 1) / 2 into arr[] modifies arr[] to { 1, 2 }.
Removing the pair (arr[0], arr[1]) from arr[] and inserting (arr[0] + arr[1] + 1) / 2 into arr[] modifies arr[] to { 2 }.
Therefore, the required output is 2.
Input: arr[] = { 30, 16, 40 }
Output: 26
Explanation:
Removing the pair (arr[0], arr[2]) from arr[] and inserting (arr[0] + arr[2] + 1) / 2 into arr[] modifies arr[] to { 16, 35 } .
Removing the pair (arr[0], arr[1]) from arr[] and inserting (arr[0] + arr[1] + 1) / 2 into arr[] modifies arr[] to { 26 } .
Therefore, the required output is 26.
Approach: The problem can be solved using Greedy technique. The idea is to repeatedly remove the maximum and the second-maximum array element and insert their average. Finally, print the smallest element left in the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSmallestNumLeft( int arr[], int N)
{
priority_queue< int > PQ;
for ( int i = 0; i < N; i++) {
PQ.push(arr[i]);
}
while (PQ.size() > 1) {
int top1 = PQ.top();
PQ.pop();
int top2 = PQ.top();
PQ.pop();
PQ.push((top1 + top2 + 1) / 2);
}
return PQ.top();
}
int main()
{
int arr[] = { 30, 16, 40 };
int N = sizeof (arr)
/ sizeof (arr[0]);
cout << findSmallestNumLeft(
arr, N);
return 0;
}
|
Java
import java.util.PriorityQueue;
class GFG{
static int findSmallestNumLeft( int arr[], int N)
{
PriorityQueue<Integer> PQ = new PriorityQueue<Integer>((a,b)->b-a);
for ( int i = 0 ; i < N; i++)
{
PQ.add(arr[i]);
}
while (PQ.size() > 1 )
{
int top1 = PQ.peek();
PQ.remove();
int top2 = PQ.peek();
PQ.remove();
PQ.add((top1 + top2 + 1 ) / 2 );
}
return PQ.peek();
}
public static void main(String[] args)
{
int arr[] = { 30 , 16 , 40 };
int N = arr.length;
System.out.print(findSmallestNumLeft(
arr, N));
}
}
|
Python3
def findSmallestNumLeft(arr, N):
PQ = []
for i in range (N):
PQ.append(arr[i])
PQ = sorted (PQ)
while ( len (PQ) > 1 ):
top1 = PQ[ - 1 ]
del PQ[ - 1 ]
top2 = PQ[ - 1 ]
del PQ[ - 1 ]
PQ.append((top1 + top2 + 1 ) / / 2 )
PQ = sorted (PQ)
return PQ[ - 1 ]
if __name__ = = '__main__' :
arr = [ 30 , 16 , 40 ]
N = len (arr)
print (findSmallestNumLeft(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GfG
{
static int findSmallestNumLeft( int [] arr, int N)
{
List< int > PQ = new List< int >();
for ( int i = 0; i < N; i++) {
PQ.Add(arr[i]);
}
PQ.Sort();
PQ.Reverse();
while (PQ.Count > 1) {
int top1 = PQ[0];
PQ.RemoveAt(0);
int top2 = PQ[0];
PQ.RemoveAt(0);
PQ.Add((top1 + top2 + 1) / 2);
PQ.Sort();
PQ.Reverse();
}
return PQ[0];
}
public static void Main()
{
int [] arr = { 30, 16, 40 };
int N = arr.Length;
Console.Write(findSmallestNumLeft(arr, N));
}
}
|
Javascript
<script>
function findSmallestNumLeft(arr, N)
{
let PQ = [];
for (let i = 0; i < N; i++)
{
PQ.push(arr[i]);
}
PQ.sort( function (a,b){ return b-a;});
while (PQ.length > 1)
{
let top1 = PQ[0];
PQ.shift();
let top2 = PQ[0];
PQ.shift();
PQ.push(Math.floor((top1 + top2 + 1) / 2));
PQ.sort( function (a,b){ return b-a;});
}
return PQ[0];
}
let arr = [ 30, 16, 40];
let N = arr.length;
document.write(findSmallestNumLeft(
arr, N));
</script>
|
Time Complexity: O(N*logN), as we are using a loop to traverse N times and priority queue operation will take logN time.
Auxiliary Space: O(N), as we are using extra space for priority queue.
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...