Repeatedly combine the two smallest values until every value in the array is at least a given threshold, then return the number of operations needed.
Problem
You are given an array of integers and a threshold value k.
In one operation, choose the two smallest values in the array, remove them, and create a new value using the rule:
Insert the new value back into the array.
Return the minimum number of operations needed so that every value in the array is at least k.
It is guaranteed that the process can always reach the threshold.
Notes
- Always combine the two smallest values available.
- Count how many such operations are performed until the minimum element in the array is at least
k. - The result can grow quickly, so use a data structure that supports efficient extraction of the smallest elements.
Input Format
nums: an integer array.k: an integer threshold.
The exact platform signature is not provided here, but the task is equivalent to repeatedly applying the operation above on the array.
Output Format
Return the minimum number of operations required so that all elements are at least k.
Constraints
- The array contains at least 1 element.
- Values are integers.
- The process is guaranteed to be solvable.
- A priority queue / min-heap is the natural fit for efficient selection of the two smallest values.
Example 1
Input
nums = [1, 2, 3, 9, 10, 12], k = 7
Output
2
Explanation
Combine 1 and 2 to get 4, giving [3, 4, 9, 10, 12]. Then combine 3 and 4 to get 10, giving [9, 10, 10, 12]. Now every value is at least 7, so the answer is 2.
Example 2
Input
nums = [5, 6, 7], k = 5
Output
0
Explanation
All values already meet the threshold, so no operations are needed.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.