title: Largest Sum Contiguous Subarray 2
signature: largest_sum_contiguous_subarray_2 <- function(a, size) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(a, size, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(a, size), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(1L, 3L, 4L, 7L, 8L, 8L, 10L, 12L, 16L, 19L, 19L, 20L, 20L, 21L, 21L, 22L, 26L, 27L, 29L, 34L, 36L, 38L, 38L, 39L, 41L, 43L, 44L, 47L, 47L, 49L, 57L, 57L, 60L, 62L, 63L, 65L, 75L, 77L, 77L, 78L, 81L, 82L, 82L, 83L, 83L, 84L, 85L, 98L, 99L), 38L, 
          1265L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-40L, 14L, 2L, -70L, 86L, -90L, -50L, -54L, -2L, 90L, 30L), 10L, 
          90L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 18L, 
          11L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(60L, 69L, 41L, 7L, 77L, 36L, 36L, 26L, 35L, 30L, 64L, 75L, 3L, 35L, 60L, 71L, 29L, 47L, 15L, 29L, 43L, 88L, 56L, 22L, 9L, 45L, 40L, 50L, 52L), 25L, 
          1063L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-96L, -88L, -80L, -72L, -64L, -64L, -60L, -60L, -60L, -58L, -56L, -54L, -54L, -50L, -50L, -26L, -26L, -24L, -20L, -8L, -2L, 0L, 4L, 4L, 12L, 14L, 18L, 18L, 24L, 32L, 42L, 44L, 44L, 44L, 48L, 50L, 50L, 56L, 60L, 60L, 70L, 80L, 88L, 88L, 90L, 98L), 35L, 
          348L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L), 22L, 
          13L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(2L, 4L, 4L, 5L, 6L, 7L, 11L, 12L, 14L, 18L, 23L, 24L, 27L, 28L, 33L, 36L, 37L, 38L, 39L, 40L, 41L, 41L, 48L, 48L, 52L, 61L, 64L, 66L, 66L, 77L, 79L, 82L, 85L, 88L, 91L, 94L, 99L), 34L, 
          1306L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-56L, 0L, 16L, 12L, 20L, 36L, 32L, -52L, -68L, -36L, -96L, -46L, -34L, 56L, 2L, 78L, 6L, 30L, -68L, -48L, 2L, 44L, -26L, -36L, -30L, -20L, -90L, -66L, 4L, 94L, 8L, 4L, -4L, -32L, -24L), 20L, 
          172L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 22L, 
          10L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(39L, 87L, 27L, 89L, 26L, 25L, 80L, 82L, 21L, 25L, 55L, 27L, 20L, 81L, 47L, 79L, 26L, 72L, 10L, 11L, 90L, 89L), 21L, 
          1019L
      )))
  
  }
  
  main()
source_py: def largest_sum_contiguous_subarray_2(a, size):
      max_so_far = a[0]
      curr_max = a[0]
      for i in range(1, size):
          curr_max = max(a[i], (curr_max + a[i]))
          max_so_far = max(max_so_far, curr_max)
      return max_so_far
source_cpp: int largest_sum_contiguous_subarray_2(vector<int> a, int size) {
    int max_so_far = a [ 0 ];
    int curr_max = a [ 0 ];
    for ( int i = 1;
    i < size;
    i ++ ) {
      curr_max = max ( a [ i ], curr_max + a [ i ] );
      max_so_far = max ( max_so_far, curr_max );
    }
    return max_so_far;
  }