fork download
  1. #include <iostream>
  2. using namespace std;
  3. #include<bits/stdc++.h>
  4.  
  5. int main() {
  6. // your code goes here
  7. int arr[] = {-2,1,-3,4,-1,2,1,-5,4};
  8. int n = sizeof(arr)/sizeof(int);
  9. //there are i+1 subarrays ending at i we have to find the maximum subarray sum ending at i
  10. vector<int>p1; int maxsum = INT_MIN; int curr_sum = arr[0];
  11. for(int i = 1 ; i <n ; i++){
  12. curr_sum = max({curr_sum+arr[i],arr[i],0}); //curr_sum = p1[i] =max( p[i-i]+ arr[i],arr[i],0)
  13. maxsum =max(curr_sum,maxsum);
  14.  
  15. }
  16. cout<<maxsum;
  17.  
  18. return 0;
  19. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
6