Merge first two minimum elements of the array until all the elements are greater than K
Last Updated :
07 May, 2022
Given an array arr[] and an integer K, the task is to find the number of merge operation required such that all the elements of the array is greater than or equal to K.
Merge Process of the Element –
New Element =
1 * (First Minimum element) +
2 * (Second Minimum element)
Examples:
Input: arr[] = {1, 2, 3, 9, 10, 12}, K = 7
Output: 2
Explanation:
After the first merge operation elements 1 and 2 is removed,
and the element (1*1 + 2*2 = 5) is inserted into the array
{3, 5, 9, 10, 12}
After the second merge operation elements 3 and 5 is removed,
and the element (3*1 + 5*2 = 13) is inserted into the array
{9, 10, 12, 13}
Thus, 2 operations are required such that all elements are greater than K.
Input: arr[] = {52, 96, 13, 37}, K = 10
Output: 0
Explanation:
All the elements of the array are greater than K already.
Therefore, no merge operation is required.
Approach: The idea is to use Min-Heap to store the elements and then merge the two minimum elements of the array until the minimum element of the array is greater than or equal to K.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int arr[], int K,
int size)
{
int least, second_least,
min_operations = 0,
new_ele = 0, flag = 0;
priority_queue< int , vector< int >,
greater< int > > heap;
for ( int i = 0; i < size; i++) {
heap.push(arr[i]);
}
while (heap.size() != 1) {
if (heap.top() >= K) {
flag = 1;
break ;
}
least = heap.top();
heap.pop();
second_least = heap.top();
heap.pop();
new_ele = (1 * least) +
(2 * second_least);
min_operations++;
heap.push(new_ele);
}
if (heap.top() >= K) {
flag = 1;
}
if (flag == 1) {
return min_operations;
}
return -1;
}
int main()
{
int N = 6, K = 7;
int arr[] = { 1, 2, 3, 9, 10, 12 };
int size = sizeof (arr) / sizeof (arr[0]);
cout << minOperations(arr, K, size);
return 0;
}
|
Java
import java.util.Collections;
import java.util.PriorityQueue;
class GFG{
static int minOperations( int arr[], int K,
int size)
{
int least, second_least,
min_operations = 0 ,
new_ele = 0 , flag = 0 ;
PriorityQueue<Integer> heap = new PriorityQueue<>();
for ( int i = 0 ; i < size; i++)
{
heap.add(arr[i]);
}
while (heap.size() != 1 )
{
if (heap.peek() >= K)
{
flag = 1 ;
break ;
}
least = heap.poll();
second_least = heap.poll();
new_ele = ( 1 * least) +
( 2 * second_least);
min_operations++;
heap.add(new_ele);
}
if (heap.peek() >= K)
{
flag = 1 ;
}
if (flag == 1 )
{
return min_operations;
}
return - 1 ;
}
public static void main(String[] args)
{
int N = 6 , K = 7 ;
int arr[] = { 1 , 2 , 3 , 9 , 10 , 12 };
int size = arr.length;
System.out.println(minOperations(arr, K, size));
}
}
|
Python3
import heapq as hq
def minOperations(arr, K, size):
least, second_least = 0 , 0
min_operations = 0
new_ele, flag = 0 , 0
hq.heapify(arr)
while len (arr) > 0 :
if arr[ 0 ] > = K:
flag = 1
break
least = arr[ 0 ]
arr[ 0 ] = arr[ - 1 ]
arr.pop()
hq.heapify(arr)
second_least = arr[ 0 ]
arr[ 0 ] = arr[ - 1 ]
arr.pop()
new_ele = least + 2 * second_least
min_operations + = 1
arr.append(new_ele)
hq.heapify(arr)
if arr[ 0 ] > = K:
flag = 1
if flag = = 1 :
return min_operations
return - 1
K = 7
arr = [ 1 , 2 , 3 , 9 , 10 , 12 ]
size = len (arr)
print (minOperations(arr, K, size))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int minOperations( int [] arr, int K, int size)
{
int least, second_least, min_operations = 0,
new_ele = 0, flag = 0;
List< int > heap = new List< int >();
for ( int i = 0; i < size; i++)
{
heap.Add(arr[i]);
}
heap.Sort();
heap.Reverse();
while (heap.Count != 1)
{
if (heap[heap.Count - 1] >= K)
{
flag = 1;
break ;
}
least = heap[heap.Count - 1];
heap.RemoveAt(heap.Count - 1);
second_least = heap[heap.Count - 1];
heap.RemoveAt(heap.Count - 1);
new_ele = (1 * least) +(2 * second_least);
min_operations++;
heap.Add(new_ele);
heap.Sort();
heap.Reverse();
}
if (heap[heap.Count - 1] >= K)
{
flag = 1;
}
if (flag == 1)
{
return min_operations;
}
return -1;
}
static public void Main ()
{
int K = 7;
int [] arr = { 1, 2, 3, 9, 10, 12 };
int size = arr.Length;
Console.WriteLine(minOperations(arr, K, size));
}
}
|
Javascript
<script>
function minOperations(arr,K,size)
{
let least, second_least,
min_operations = 0,
new_ele = 0, flag = 0;
let heap = [];
for (let i = 0; i < size; i++)
{
heap.push(arr[i]);
}
while (heap.length != 1)
{
if (heap[0] >= K)
{
flag = 1;
break ;
}
least = heap.shift();
second_least = heap.shift();
new_ele = (1 * least) +
(2 * second_least);
min_operations++;
heap.push(new_ele);
}
if (heap[0] >= K)
{
flag = 1;
}
if (flag == 1)
{
return min_operations;
}
return -1;
}
let N = 6, K = 7;
let arr=[1, 2, 3, 9, 10, 12];
let size = arr.length;
document.write(minOperations(arr, K, size));
</script>
|
Time Complexity: Complexity of the above algorithms is determined by one by while loop and second by heapify operation. Time taken by while loop is O(N) and inside the same while loop heapify() operation is used that will take
O(Log n) time, so overall time complexity will be O(N* Log N).
Space Complexity: O(N) space would be required for the storing elements into heap.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...