![]() |
VOOZH | about |
Given an array arr[] of size n and an integer k, choose two elements from the array erase them and insert the sum of these two elements in the array until all the elements are greater than or equal to k. Find the minimum number of such operations required.
Examples:
Input: arr[] = [1 10 12 9 2 3], k = 6
Output: 2
Explanation: First we add (1 + 2), now the new list becomes 3 10 12 9 3, then we add (3 + 3), now the new list becomes 6 10 12 9, Now all the elements in the list are greater than 6. Hence the output is 2 i:e 2 operations are required to do this.Input:
arr[] = [1, 2, 3, 4],k = 10
Output:3
Table of Content
If we take a closer look, we can notice that this problem is similar to Huffman coding. The strategy uses a Min-Heap to repeatedly combine the two smallest elements into a single sum until all values are at least k. By always merging the smallest available numbers, we "boost" the lowest values toward the threshold in the fewest operations possible.
For example arr[] = [1 10 12 9 2 3], k = 6
Output: 2
2