fork download
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int arr[] = {-2, -3, 4, -1, -2, 1, 5, -3};
  6.  
  7. int best = -1110;
  8. int sum = 0;
  9. int indx = 0;
  10. int i = 0;
  11. int x = 0;
  12. int y = 0;
  13. int n = 8;
  14.  
  15. for (i = 0; i < n; i++) {
  16. if (sum <= 0) {
  17. sum = arr[i];
  18. indx = i;
  19. } else {
  20. sum += arr[i];
  21. }
  22.  
  23. if (best < sum) {
  24. best = sum;
  25. x = indx;
  26. y = i;
  27. }
  28. }
  29.  
  30. printf("Max sum %d, start index %d, end index %d\n", best, x, y);
  31.  
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
Max sum 7, start index 2, end index 6