Advertisement
STANAANDREY

maxSumSeq

Aug 26th, 2019
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int getMaxSum(int *a, int n)
  5. {
  6.     int bestSum = a[0];
  7.     int best[100];
  8.     for (int i = 0; i < n; i++)
  9.     {
  10.         best[i] = max(a[i], best[i - 1] + a[i]);
  11.         bestSum = max(best[i], bestSum);
  12.     }
  13.     return bestSum;
  14. }
  15.  
  16. int main()
  17. {
  18.     int a[]= {-1, 2, 3, -4, -2, 2, 1, -3, -2, -3, -4, 9, -2, 1, 7, 8, -19, -7, 2, 4, 3};
  19.     cout << getMaxSum(a, 21);
  20.     return 0;
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement