Adding Array Elements | Practice | GeeksforGeeks

Given an array Arr[] of size N and an integer K, you have to add the first two minimum elements of the array until all the elements are greater than or equal to K and find the number of such operations required.
Example 1:
Input:
N = 6, K = 6 
Arr[] = {1, 10, 12, 9, 2, 3}
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. 

Example 2:
Input:
N = 4, K = 4
Arr[] = {5, 4, 6, 4}
Output: 0
Explanation: Every element in the given array 
is greater than or equal to K.

Your Task:
You don't need to read input or print anything. Your task is to complete the function minOperations() which takes array of integers arr, n and k as parameters and returns an integer denoting the answer. If it is not possible to make elements greater than or equal to K, return -1.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints :
1 ≤ N, K ≤ 105
1 ≤ Arr[i] ≤ 106
 
0
hharshit81182 weeks ago
Best Solution : O(nlogn)
class Solution{ public:   int minOperations(int arr[], int n, int k) {       priority_queue<int, vector<int>, greater<int>> p;       for(int i=0; i< n; i++){           p.push(arr[i]);       }       int count = 0;       while(p.top() < k){           int curr = p.top(); p.pop();           curr += p.top(); p.pop();           p.push(curr);           count++;       }       return count;   }};
+1
yashchawla1162 months ago
Simple To Understand And Easy To Implement.
 
https://yashboss116.blogspot.com/2022/03/adding-array-elements-geeks-for-geeks.html
 
 
+1
himanshukug19cs2 months ago
java solution
 
  PriorityQueue<Integer> pq = new PriorityQueue<>();       for(int i=0;i<n;i++){           pq.add(arr[i]);       }       int ans=0;       while(pq.peek()<k){           int a = pq.poll();           a+=pq.poll();           pq.add(a);           ans++;       }       return ans;
0
aharshit3502 months ago
JAVA SOLUTION
class Solution {   int minOperations(int[] arr, int n, int k) {       // code here       PriorityQueue<Integer>pq=new PriorityQueue<>();       for(int i=0;i<n;i++){           pq.add(arr[i]);       }int result=0;       while(k>pq.peek()){           int x=pq.poll();           int y=pq.poll();           int z=x+y;           pq.add(z);           result++;       }return result;   }} 
0
excuses3 months ago
///JAVA SOLUTION///
TIME TAKEN -:) 0.9 SEC .
 
 int c=0;     PriorityQueue<Integer>heap=new PriorityQueue<>();     for(int i=0;i<n;i++)     {         heap.add(arr[i]);     }        if(heap.peek()>=k)    {        return 0;    }    else    {     while(heap.peek()<k)     {        int a=heap.poll();        int b=heap.poll();        int sum=a+b;        c++;        heap.add(sum);     }    }    return c;
+1
sudarshanp49123 months ago
EASY SOLUTION:
int minOperations(int arr[], int n, int k)
    {
        priority_queue<int, vector<int>, greater<int>> minh;
        int count=0,sum=0;
        
        for(int i=0;i<n;i++)
        {
            minh.push(arr[i]);
            sum+=arr[i];
        }
        
        while(minh.top()<k and minh.size()>=2)
        {
            int x=minh.top();
            minh.pop();
            int y=minh.top();
            minh.pop();
            minh.push(x+y);
            count++;
        }
        if(sum<k)
        return -1;
        
        return count;
    }
0
imranwahid5 months ago
Easy C++ solution
0
imranwahid
This comment was deleted.
+1
badgujarsachin836 months ago
int minOperations(int arr[], int n, int k) {
       // code here
       int operation=0;
       priority_queue<int,vector<int>,greater<int>> p;
       for(int i=0;i<n;i++)
       {
           p.push(arr[i]);
       }
       while(!p.empty()){
           int a=p.top();
           p.pop();
           int b=p.top();
           p.pop();
           int sum=a+b;
           if(a>=k and b>=k){
               break;
           }
           p.push(sum);
           operation++;
       }
       return operation;
   }
-1
rohitpendse1386 months ago
class Solution {public:   int minOperations(int arr[], int n, int k) {       // code here       priority_queue<int, vector<int>, greater<int>> mnh;       for (int i = 0; i < n; i++) {           mnh.push(arr[i]);       }       int ans = 0;       while (!mnh.empty()) {           int x = mnh.top();           mnh.pop();           int y = mnh.top();           mnh.pop();           if (x >= k and y >= k) break;           mnh.push(x + y);           ans++;       }       return ans;   }};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.