title: Maximum Subarray Sum Using Prefix Sum
signature: fun maximumSubarraySumUsingPrefixSum(arr: ArrayList<Int>, n: Int): Int {
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;
  }
question_info.test_list: [{"idx": 0, "outputs": 633, "inputs": {"arr": [8, 9, 11, 17, 18, 19, 23, 24, 27, 30, 31, 31, 35, 44, 46, 47, 49, 51, 55, 58, 59, 61, 65, 67, 71, 71, 71, 71, 78, 78, 82, 91, 98], "n": 20}}, {"idx": 1, "outputs": 114, "inputs": {"arr": [-82, -28, -66, -52, -36, 36, -88, 52, -62, 46, 42, 26, -60, 18, -52, 38, 94, -68, 44, -94, 14, 36, -70], "n": 15}}, {"idx": 2, "outputs": 8, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 19}}, {"idx": 3, "outputs": 999, "inputs": {"arr": [28, 36, 42, 42, 5, 52, 74, 86, 55, 82, 59, 81, 4, 90, 24, 34, 20, 99, 86, 25, 52, 48, 62, 5, 67, 83, 60, 72, 80, 73, 38, 55, 8, 70, 95], "n": 19}}, {"idx": 4, "outputs": -24, "inputs": {"arr": [-92, -52, -24, 36, 56], "n": 3}}, {"idx": 5, "outputs": 7, "inputs": {"arr": [0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], "n": 13}}, {"idx": 6, "outputs": 811, "inputs": {"arr": [1, 1, 4, 4, 7, 7, 17, 18, 20, 26, 26, 32, 37, 38, 42, 44, 44, 46, 50, 53, 57, 58, 58, 60, 61, 61, 64, 74, 75, 77, 83, 83, 84, 84, 85, 87, 88, 90, 95, 96, 97, 98, 99, 99], "n": 25}}, {"idx": 7, "outputs": 204, "inputs": {"arr": [-86, 2, 26, 54, -16, 16, 48, 24, 50, -10, -32, -62, 48, -12, -66], "n": 13}}, {"idx": 8, "outputs": 0, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 14}}, {"idx": 9, "outputs": 1971, "inputs": {"arr": [58, 14, 79, 11, 31, 28, 61, 86, 25, 27, 75, 78, 32, 55, 86, 48, 15, 51, 6, 78, 23, 82, 16, 62, 35, 51, 91, 16, 79, 38, 97, 30, 23, 58, 95, 57, 82, 35, 57, 43, 22, 41, 58, 69, 25, 65, 13, 79], "n": 39}}]
question_info.extension: kts
question_info.test_code: package main
  
  import kotlin.text.*
  import kotlin.math.*
  
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  fun validateSolution(actual:Int, expected:Int):Boolean{
    return actual.equals(expected)
  }
  fun driver(arr: ArrayList<Int>, n: Int, expected:Int):String {
      try{
        if (validateSolution(PLACEHOLDER_FN_NAME(arr, n), expected)) {
                return "PASSED"
        }
        return "FAILED"
      } catch (e: Exception){
        return e::class.simpleName as String
      }
  
  }
  
  fun main() {
      var result: String = ""
      result = driver(arrayListOf(8, 9, 11, 17, 18, 19, 23, 24, 27, 30, 31, 31, 35, 44, 46, 47, 49, 51, 55, 58, 59, 61, 65, 67, 71, 71, 71, 71, 78, 78, 82, 91, 98), 20, 633)
      println("TEST-0...${result}")
      
      result = driver(arrayListOf(-82, -28, -66, -52, -36, 36, -88, 52, -62, 46, 42, 26, -60, 18, -52, 38, 94, -68, 44, -94, 14, 36, -70), 15, 114)
      println("TEST-1...${result}")
      
      result = driver(arrayListOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 19, 8)
      println("TEST-2...${result}")
      
      result = driver(arrayListOf(28, 36, 42, 42, 5, 52, 74, 86, 55, 82, 59, 81, 4, 90, 24, 34, 20, 99, 86, 25, 52, 48, 62, 5, 67, 83, 60, 72, 80, 73, 38, 55, 8, 70, 95), 19, 999)
      println("TEST-3...${result}")
      
      result = driver(arrayListOf(-92, -52, -24, 36, 56), 3, -24)
      println("TEST-4...${result}")
      
      result = driver(arrayListOf(0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0), 13, 7)
      println("TEST-5...${result}")
      
      result = driver(arrayListOf(1, 1, 4, 4, 7, 7, 17, 18, 20, 26, 26, 32, 37, 38, 42, 44, 44, 46, 50, 53, 57, 58, 58, 60, 61, 61, 64, 74, 75, 77, 83, 83, 84, 84, 85, 87, 88, 90, 95, 96, 97, 98, 99, 99), 25, 811)
      println("TEST-6...${result}")
      
      result = driver(arrayListOf(-86, 2, 26, 54, -16, 16, 48, 24, 50, -10, -32, -62, 48, -12, -66), 13, 204)
      println("TEST-7...${result}")
      
      result = driver(arrayListOf(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 14, 0)
      println("TEST-8...${result}")
      
      result = driver(arrayListOf(58, 14, 79, 11, 31, 28, 61, 86, 25, 27, 75, 78, 32, 55, 86, 48, 15, 51, 6, 78, 23, 82, 16, 62, 35, 51, 91, 16, 79, 38, 97, 30, 23, 58, 95, 57, 82, 35, 57, 43, 22, 41, 58, 69, 25, 65, 13, 79), 39, 1971)
      println("TEST-9...${result}")
      
          
  }
  
  main()
question_info.entry_fn_name: maximumSubarraySumUsingPrefixSum
question_info.entry_cls_name: Solution
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: kotlinc -script __FILENAME__ -no-reflect -nowarn