Advertisement
maycod23

Untitled

Apr 4th, 2023
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. //given an n length array and k, you eed to find the max subarray sum of length k.
  5.  
  6. int n, k; cin >> n >> k;
  7. vector<int> v(n);
  8. for (int i = 0; i < n; i++) cin >> v[i];
  9. int ans = INT_MIN, tempsum = 0, s = 0;
  10. for (int i = 0; i < n; i++) {
  11. tempsum += v[i];
  12. if ((i - s + 1) == k) {
  13. ans = max(ans, tempsum);
  14. tempsum -= v[s];
  15. s++;
  16. }
  17. }
  18. cout << ans << endl;
  19.  
  20. //follow-up questions
  21. /*
  22. 1.>find number of windows having maximum sum
  23. 2.>find the range of window having maxium sum.
  24. 3.>find the range of window having maximum sum, but it can be as nearer to the start.
  25. 4.>find the range of window having maximum sum, but it can be as nearer to the end.
  26. */
  27. return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement