fork download
  1. #include <stdio.h>
  2.  
  3. #define MIN_INT -32768
  4.  
  5. int largest_contiguous_sum(int *array, int size) {
  6. int max = MIN_INT;
  7. int current_max = MIN_INT;
  8. for (int i = 0; i < size; i++) {
  9. current_max = current_max + array[i] > array[i] ? current_max + array[i] : array[i];
  10. max = max > current_max ? max : current_max;
  11. }
  12. return max;
  13. }
  14.  
  15. int main() {
  16. int myridium_array[] = {5,5,5,-1,-1,5,5,5};
  17. int size = 8;
  18. printf("The largest sub-array sum is: %d\n", largest_contiguous_sum(myridium_array, size));
  19. }
  20.  
Success #stdin #stdout 0s 2168KB
stdin
Standard input is empty
stdout
The largest sub-array sum is: 28