Adding elements of an array until every element becomes greater than or equal to k (original) (raw)

Last Updated : 24 Apr, 2026

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

Try It Yourselfredirect icon

Table of Content

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

[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

Output: 2

C++ `

#include #include #include #include

using namespace std;

// Function to count minimum steps to make all elements >= k int minOps(vector &arr, int k) {

// Min-heap to always access the two smallest elements efficiently
priority_queue<int, vector<int>, greater<int>> pq;

// Push all array elements into the priority queue
for (int i = 0; i < arr.size(); i++) {
    pq.push(arr[i]);
}

int count = 0;

// Continue combining elements while the smallest is less than k
// and there are at least two elements to combine
while (pq.size() > 1 && pq.top() < k) {
    
    // Extract the two smallest elements
    int min1 = pq.top();
    pq.pop();
    
    int min2 = pq.top();
    pq.pop();

    // Combine them and push the result back into the heap
    int sum = min1 + min2;
    pq.push(sum);
    
    // Increment the operation counter
    count++;
}

// After operations, if the remaining smallest element is still < k,
// it's impossible to meet the condition
if (pq.top() < k) {
    return -1;
}

return count;

}

int main() { vector arr = {1, 10, 12, 9, 2, 3}; int k = 6;

cout << minOps(arr, k) << "\n";

return 0;

}

Java

import java.util.PriorityQueue;

public class GFG {

// Function to count minimum steps to make all elements >= k
static int minOps(int arr[], int k) {
    
    int n = arr.length ;
    
    // Use a PriorityQueue (Min-Heap) to always get the smallest elements
    PriorityQueue<Integer> pq = new PriorityQueue<>();

    // Add all elements of the array to the priority queue
    for (int i = 0; i < n; i++) {
        
        pq.add(arr[i]);
    }

    int count = 0;

    // Combine the two smallest elements if the smallest is still less than k
    while (pq.size() > 1 && pq.peek() < k) {
        
        // Retrieve and remove the two smallest elements
        int min1 = pq.poll();
        int min2 = pq.poll();

        // Calculate the sum and add it back to the queue
        int sum = min1 + min2;
        pq.add(sum);

        // Increment operation count
        count++;
    }

    // Check if the remaining smallest element meets the threshold k
    if (pq.peek() < k) {
        return -1;
    }

    return count;
}

public static void main(String[] args) {
    int arr[] = { 1, 10, 12, 9, 2, 3 };
    int k = 6;

    System.out.println(minOps(arr, k));
}

}

Python

import heapq

def minOps(arr, k):

heapq.heapify(arr)
    
count = 0
    
# Continue while at least two elements exist and the smallest is < k
while len(arr) > 1 and arr[0] < k:
    
    # Extract the two smallest elements
    min1 = heapq.heappop(arr)
    min2 = heapq.heappop(arr)
        
    # Combine elements and push the result back
    new_element = min1 + min2
    heapq.heappush(arr, new_element)
        
    count += 1
        
# Final check: if the remaining element is still less than k, return -1
if not arr or arr[0] < k:
    return -1
        
return count

if name == "main": arr = [1, 10, 12, 9, 2, 3] k = 6

print(minOps(arr, k))

C#

using System; using System.Collections.Generic;

public class GFG { static int minOps(int[] arr, int k) {

    // PriorityQueue stores elements and sorts them by priority (the value itself)
    // We use int for both the element and its priority to create a Min-Heap
    PriorityQueue<int, int> pq = new PriorityQueue<int, int>();

    // Enqueue all elements into the priority queue
    foreach (int value in arr) {
        
        pq.Enqueue(value, value);
    }

    int count = 0;

    // Continue if there are at least 2 elements and the smallest is less than k
    while (pq.Count > 1 && pq.Peek() < k) {
        
        // Extract the two smallest elements
        int min1 = pq.Dequeue();
        int min2 = pq.Dequeue();

        // Combine them and add the result back to the heap
        int sum = min1 + min2;
        pq.Enqueue(sum, sum);

        count++;
    }

    // If the smallest remaining element is still less than k, return -1
    if (pq.Count > 0 && pq.Peek() < k) {
        return -1;
    }

    return count;
}

public static void Main() {
    int[] arr = { 1, 10, 12, 9, 2, 3 };
    int k = 6;

    Console.WriteLine(minOps(arr, k));
}

}

JavaScript

// Function to count minimum steps to make all elements >= k function minOps(arr, k) {

// Sort array in descending order so we can pop from the end (efficient)
arr.sort((a, b) => b - a);

let count = 0;

// Continue while at least two elements exist and the smallest is < k
while (arr.length > 1 && arr[arr.length - 1] < k) {
    
    // Take the two smallest elements (from the end of sorted array)
    let min1 = arr.pop();
    let min2 = arr.pop();

    // Combine them and push the sum back
    let sum = min1 + min2;
    arr.push(sum);

    // Re-sort to keep the smallest elements at the end
    arr.sort((a, b) => b - a);
    
    count++;
}

// If the remaining smallest element is still less than k, return -1
return arr[arr.length - 1] < k ? -1 : count;

}

// Driver code let arr = [1, 10, 12, 9, 2, 3]; let k = 6; console.log(minOps(arr, k));

`