Advertisement
newb_ie

Prefix Sum

Nov 9th, 2021
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2.  
  3. using namespace std;
  4.  
  5. int main () {
  6. ios::sync_with_stdio(false);
  7. cin.tie(nullptr);
  8. cout.tie(nullptr);
  9. int n = 5;
  10. int a[n + 1];
  11. for (int i = 1; i <= n; ++i) {
  12. cin >> a[i];
  13. }
  14. int p[n + 1];
  15. p[0] = 0;
  16. int sum = 0;
  17. for (int i = 1; i <= n; ++i) {
  18. sum += a[i];
  19. p[i] = sum;
  20. }
  21. int L = 2, R = 4;
  22. cout << p[R] - p[L - 1] << '\n';
  23. }
  24.  
  25. //1 2 3 4 5
  26. //1 2 3 4 5 = (L = 2, R = 4) => 9
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement