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++
// C++ implementation to merge the // elements of the array untill all // the array element of the array // greater than or equal to K #include <bits/stdc++.h> using namespace std; // Function to find the minimum // operation required to merge // elements of the array int minOperations(int arr[], int K, int size) { int least, second_least, min_operations = 0, new_ele = 0, flag = 0; // Heap to store the elements // of the array and to extract // minimum elements of O(logN) priority_queue<int, vector<int>, greater<int> > heap; // Loop to push all the elements // of the array into heap for (int i = 0; i < size; i++) { heap.push(arr[i]); } // Loop to merge the minimum // elements untill there is only // all the elements greater than K while (heap.size() != 1) { // Condition to check minimum // element of the array is // greater than the K if (heap.top() >= K) { flag = 1; break; } // Merge the two minimum // elements of the heap 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; } // Driver Code 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; } |
2
Recommended Posts:
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Merge K minimum elements of the array until there is only one element
- Adding elements of an array until every element becomes greater than or equal to k
- Smallest subarray of size greater than K with sum greater than a given value
- Sum of all array elements less than X and greater than Y for Q queries
- Count of elements in first Array greater than second Array with each element considered only once
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Longest subsequence with first and last element greater than all other elements
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Reduce the array by deleting elements which are greater than all elements to its left
- Count of array elements which are greater than all elements on its left
- Find K such that changing all elements of the Array greater than K to K will make array sum N
- Find the element before which all the elements are smaller than it, and after which all are greater
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
- Find the winner by adding Pairwise difference of elements in the array until Possible
- Find minimum value to assign all array elements so that array product becomes greater
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


