Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int main() {
- //given an n length array and k, you eed to find the max subarray sum of length k.
- int n, k; cin >> n >> k;
- vector<int> v(n);
- for (int i = 0; i < n; i++) cin >> v[i];
- int ans = INT_MIN, tempsum = 0, s = 0;
- for (int i = 0; i < n; i++) {
- tempsum += v[i];
- if ((i - s + 1) == k) {
- ans = max(ans, tempsum);
- tempsum -= v[s];
- s++;
- }
- }
- cout << ans << endl;
- //follow-up questions
- /*
- 1.>find number of windows having maximum sum
- 2.>find the range of window having maxium sum.
- 3.>find the range of window having maximum sum, but it can be as nearer to the start.
- 4.>find the range of window having maximum sum, but it can be as nearer to the end.
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement