fork download
  1. #include <iostream>
  2. #include <limits.h>
  3. using namespace std;
  4.  
  5. int maxSubArray(int* nums, int numsSize) {
  6. int curSum, maxSum = INT_MIN;
  7. curSum = 0;
  8. for(int i = 0; i < numsSize; i++){
  9. curSum = curSum + nums[i];
  10. if(curSum > maxSum){
  11. maxSum = curSum;
  12. }
  13. if(curSum < 0){
  14. curSum = 0;
  15. }
  16. }
  17. return maxSum;
  18. }
  19.  
  20. int main() {
  21. int myints[] = {10, 0, 30, 0, -50, 60, -90, 80};
  22. cout << maxSubArray(myints, 8) << endl;
  23. return 0;
  24. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
80