title: Maximum Subarray Sum Using Prefix Sum
signature: maximum_subarray_sum_using_prefix_sum <- 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(8L, 9L, 11L, 17L, 18L, 19L, 23L, 24L, 27L, 30L, 31L, 31L, 35L, 44L, 46L, 47L, 49L, 51L, 55L, 58L, 59L, 61L, 65L, 67L, 71L, 71L, 71L, 71L, 78L, 78L, 82L, 91L, 98L), 20L, 
          633L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-82L, -28L, -66L, -52L, -36L, 36L, -88L, 52L, -62L, 46L, 42L, 26L, -60L, 18L, -52L, 38L, 94L, -68L, 44L, -94L, 14L, 36L, -70L), 15L, 
          114L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(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, 1L, 1L), 19L, 
          8L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(28L, 36L, 42L, 42L, 5L, 52L, 74L, 86L, 55L, 82L, 59L, 81L, 4L, 90L, 24L, 34L, 20L, 99L, 86L, 25L, 52L, 48L, 62L, 5L, 67L, 83L, 60L, 72L, 80L, 73L, 38L, 55L, 8L, 70L, 95L), 19L, 
          999L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-92L, -52L, -24L, 36L, 56L), 3L, 
          -24L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L), 13L, 
          7L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(1L, 1L, 4L, 4L, 7L, 7L, 17L, 18L, 20L, 26L, 26L, 32L, 37L, 38L, 42L, 44L, 44L, 46L, 50L, 53L, 57L, 58L, 58L, 60L, 61L, 61L, 64L, 74L, 75L, 77L, 83L, 83L, 84L, 84L, 85L, 87L, 88L, 90L, 95L, 96L, 97L, 98L, 99L, 99L), 25L, 
          811L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-86L, 2L, 26L, 54L, -16L, 16L, 48L, 24L, 50L, -10L, -32L, -62L, 48L, -12L, -66L), 13L, 
          204L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 14L, 
          0L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(58L, 14L, 79L, 11L, 31L, 28L, 61L, 86L, 25L, 27L, 75L, 78L, 32L, 55L, 86L, 48L, 15L, 51L, 6L, 78L, 23L, 82L, 16L, 62L, 35L, 51L, 91L, 16L, 79L, 38L, 97L, 30L, 23L, 58L, 95L, 57L, 82L, 35L, 57L, 43L, 22L, 41L, 58L, 69L, 25L, 65L, 13L, 79L), 39L, 
          1971L
      )))
  
  }
  
  main()
source_py: def maximum_subarray_sum_using_prefix_sum(arr, n):
      min_prefix_sum = 0
      res = (- math.inf)
      prefix_sum = []
      prefix_sum.append(arr[0])
      for i in range(1, n):
          prefix_sum.append((prefix_sum[(i - 1)] + arr[i]))
      for i in range(n):
          res = max(res, (prefix_sum[i] - min_prefix_sum))
          min_prefix_sum = min(min_prefix_sum, prefix_sum[i])
      return res
source_cpp: int maximum_subarray_sum_using_prefix_sum(vector<int> arr, int n) {
    int min_prefix_sum = 0;
    int res = numeric_limits < int > :: min ( );
    int prefix_sum [ n ];
    prefix_sum [ 0 ] = arr [ 0 ];
    for ( int i = 1;
    i < n;
    i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ];
    for ( int i = 0;
    i < n;
    i ++ ) {
      res = max ( res, prefix_sum [ i ] - min_prefix_sum );
      min_prefix_sum = min ( min_prefix_sum, prefix_sum [ i ] );
    }
    return res;
  }