forked from Thelalitagarwal/GFG_Daily_Problem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdding Array Elements.cpp
More file actions
49 lines (43 loc) · 1.02 KB
/
Adding Array Elements.cpp
File metadata and controls
49 lines (43 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
public:
int minOperations(int arr[], int n, int k) {
priority_queue<int,vector<int>,greater<int>> heap;
for(int i=0;i<n;i++){
heap.push(arr[i]);
}
int count=0;
while(heap.size()>1 && k>heap.top()){
int minimum=heap.top();
heap.pop();
int minimum_second=heap.top();
heap.pop();
heap.push(minimum+minimum_second);
count++;
}
if(heap.top()>=k){
return count;
}
return -1;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
Solution obj;
int ans = obj.minOperations(arr, n, k);
cout << ans << "\n";
}
return 0;
}
// } Driver Code Ends