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;
  11. for(int i = 0 ; i <n ; i++){
  12. int sum = 0; int t=INT_MIN;
  13. for(int j = i ;j>=0;j--){
  14. sum+=arr[j];
  15. t=max(t,sum);
  16. }
  17. p1.push_back(t);
  18. }
  19. for(int i = 0 ; i<n;i++) cout<<p1[i]<<" ";
  20.  
  21. return 0;
  22. }
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
-2 1 -2 4 3 5 6 1 5