title: Smallest Sum Contiguous Subarray
signature: smallest_sum_contiguous_subarray <- function(arr, n) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(2L, 9L, 13L, 14L, 15L, 18L, 19L, 19L, 25L, 26L, 29L, 29L, 29L, 30L, 31L, 36L, 37L, 37L, 38L, 39L, 39L, 40L, 40L, 42L, 42L, 46L, 50L, 53L, 58L, 60L, 62L, 64L, 65L, 67L, 68L, 69L, 72L, 77L, 78L, 83L, 85L, 89L, 90L, 93L, 95L, 95L, 97L), 24L, 
          2L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(14L, -58L, 8L, 78L, -26L, -20L, -60L, 42L, -64L, -12L), 6L, 
          -58L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 8L, 
          0L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(44L, 88L, 20L, 47L, 69L, 42L, 26L, 49L, 71L, 91L, 18L, 95L, 9L, 66L, 60L, 37L, 47L, 29L, 98L, 63L, 15L, 9L, 80L, 66L, 1L, 9L, 57L, 56L, 20L, 2L, 1L), 26L, 
          1L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-78L, -64L, -62L, -60L, -52L, 4L, 8L, 46L, 72L, 74L), 8L, 
          -316L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L), 11L, 
          0L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(3L, 7L, 16L, 17L, 23L, 23L, 23L, 28L, 29L, 30L, 34L, 38L, 40L, 41L, 43L, 43L, 44L, 46L, 51L, 51L, 51L, 55L, 57L, 58L, 61L, 62L, 66L, 66L, 67L, 69L, 70L, 73L, 75L, 77L, 79L, 80L, 85L, 85L, 87L, 87L, 93L, 96L), 38L, 
          3L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(80L, 22L, 38L, 26L, 62L, -48L, -48L, 46L, -54L, 4L, 76L, 46L, 48L, 40L, -92L, -98L, -88L, 12L, -42L, -94L, 76L, -16L, -82L, 62L, 18L, -24L), 22L, 
          -402L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L), 13L, 
          0L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(85L, 44L, 1L, 97L, 50L, 74L, 62L, 90L, 3L, 78L, 8L, 48L, 96L, 41L, 36L, 91L, 57L, 97L, 85L, 90L, 78L, 43L, 28L, 92L, 85L, 59L, 29L, 38L, 34L, 65L, 20L, 26L, 27L, 23L, 71L, 22L, 47L, 99L, 68L, 93L, 67L, 66L, 69L, 82L, 98L, 15L, 44L, 51L, 65L), 45L, 
          1L
      )))
  
  }
  
  main()
source_py: def smallest_sum_contiguous_subarray(arr, n):
      min_ending_here = sys.maxsize
      min_so_far = sys.maxsize
      for i in range(n):
          if (min_ending_here > 0):
              min_ending_here = arr[i]
          else:
              min_ending_here += arr[i]
          min_so_far = min(min_so_far, min_ending_here)
      return min_so_far
source_cpp: int smallest_sum_contiguous_subarray(vector<int> arr, int n) {
    int min_ending_here = INT_MAX;
    int min_so_far = INT_MAX;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( min_ending_here > 0 ) min_ending_here = arr [ i ];
      else min_ending_here += arr [ i ];
      min_so_far = min ( min_so_far, min_ending_here );
    }
    return min_so_far;
  }