VOOZH about

URL: https://www.geeksforgeeks.org/dsa/adding-elements-array-every-element-becomes-greater-k/

⇱ Adding elements of an array until every element becomes greater than or equal to k - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Adding elements of an array until every element becomes greater than or equal to k

Last Updated : 24 Apr, 2026

Given an array arr[] of size 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

[Naive Approach] Repeated Sorting - (n*n Log n) Time and O(n) Space.

  • Sort the array, pick the two smallest elements, remove from the array and add their sum to the array.
  • Repeat the above process while the smallest element in array is smaller than k.

[Expected Approach] Using Min-Heap - O(n * log(n)) Time and O(n) Space

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

  • Initial Heap: [1, 2, 3, 9, 10, 12]
  • Operation 1: Pop 1 and 2. Push sum 3, Heap: [3, 3, 9, 10, 12] (Count = 1)
  • Operation 2: Pop 3 and 3. Push sum 6, Heap: [6, 9, 10, 12] (Count = 2)
  • Finish: Smallest element is 6, which is $\ge 6$. Stop.

Output: 2


Output
2
Comment
Article Tags:
Article Tags: