MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / main

Function main

CodeChef_problems/BINARR/solution.cpp:18–45  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16#define ll long long
17using namespace std;
18int main()
19{
20 ll n, k, s, i, j = 0, sum = 0, ans = LLONG_MAX;
21 // Taking input
22 cin >> n >> k >> s;
23 ll a[n + 2];
24 for (i = 0; i < n; i++)
25 cin >> a[i];
26 for (i = 0; i < n; i++)
27 {
28 sum += a[i];
29 //If sum remains still greater than s after subtracting the leftmost element increment
30 //the pointer by 1
31 while (sum - a[j] > s and (i - j) > k and j < i)
32 {
33 sum -= a[j];
34 j++;
35 }
36 //If all the conditions are valid update the answer
37 if (sum > s and (i - j + 1) > k)
38 ans = min(ans, (i - j + 1));
39 }
40 //If there is no such subarray make the answer as -1
41 if (ans == LLONG_MAX)
42 ans = -1;
43 //printing the output
44 cout << ans;
45}

Callers

nothing calls this directly

Calls 1

minFunction · 0.85

Tested by

no test coverage detected