MCPcopy Create free account
hub / github.com/Seogeurim/CS-study / main

Function main

contents/algorithm/code/two_pointer.cpp:10–42  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8int arr[100001];
9
10int main(void){
11 ios::sync_with_stdio(false);
12 cin.tie(0);
13
14 cin>>N>>S;
15 for(int i=0; i<N; i++){
16 cin>>arr[i];
17 }
18
19 int left = 0;
20 int right = 0;
21 int ans = 987654321;
22 int sum = arr[0];
23 while(left<=right && right<N){
24
25 if(sum < S)
26 sum += arr[++right];
27 else if(sum == S){
28 ans = min(ans, right-left+1);
29 sum += arr[++right];
30 }
31 else if(sum > S){
32 ans = min(ans, right-left+1);
33 sum -= arr[left++];
34 }
35 }
36
37 if(ans == 987654321)
38 cout<<"0\n";
39 else
40 cout<<ans<<"\n";
41 return 0;
42}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected