-
Notifications
You must be signed in to change notification settings - Fork 345
/
Shortest Subarray with Sum at least K.cpp
57 lines (43 loc) · 1.2 KB
/
Shortest Subarray with Sum at least K.cpp
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
50
51
52
53
54
55
56
57
/*
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Input: A = [2,-1,2], K = 3
Output: 3
*/
#include<bits/stdc++.h>
using namespace std;
int shortestSubarray(vector<int>& A, int K)
{
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq; // min heap to store the length and sum of the subarray
int sum = 0;
int ans = INT_MAX;
for(int i=0;i<A.size();i++){
sum += A[i];
if(sum >= K){
ans = min(ans,i+1);
}
while(pq.size() >0 && sum-pq.top().first >=K ){
ans = min(ans,i-pq.top().second);
pq.pop();
}
pq.push({sum,i});
}
return ans == INT_MAX ? -1 : ans;
}
int main()
{
int K;
cin >> K;
cout << "Enter the number of elements in the array" ;
int n;
cin >> n;
vector<int> arr(n);
for(int i=0;i<n;i++)
{
int x;
cin >> x;
arr.push_back(x);
}
cout << shortestSubarray(arr , K);
return 0;
}