title: Find Maximum Average Subarray Of K Length
signature: fun findMaximumAverageSubarrayOfKLength(arr: ArrayList<Int>, n: Int, k: Int): Int {
source_py: def find_maximum_average_subarray_of_k_length(arr, n, k):
      if (k > n):
          return (- 1)
      csum = ([0] * n)
      csum[0] = arr[0]
      for i in range(1, n):
          csum[i] = (csum[(i - 1)] + arr[i])
      max_sum = csum[(k - 1)]
      max_end = (k - 1)
      for i in range(k, n):
          curr_sum = (csum[i] - csum[(i - k)])
          if (curr_sum > max_sum):
              max_sum = curr_sum
              max_end = i
      return ((max_end - k) + 1)
source_cpp: int find_maximum_average_subarray_of_k_length(vector<int> arr, int n, int k) {
    if ( k > n ) return - 1;
    int * csum = new int [ n ];
    csum [ 0 ] = arr [ 0 ];
    for ( int i = 1;
    i < n;
    i ++ ) csum [ i ] = csum [ i - 1 ] + arr [ i ];
    int max_sum = csum [ k - 1 ], max_end = k - 1;
    for ( int i = k;
    i < n;
    i ++ ) {
      int curr_sum = csum [ i ] - csum [ i - k ];
      if ( curr_sum > max_sum ) {
        max_sum = curr_sum;
        max_end = i;
      }
    }
    delete [ ] csum;
    return max_end - k + 1;
  }
question_info.test_list: [{"idx": 0, "outputs": 9, "inputs": {"arr": [2, 4, 6, 19, 21, 23, 32, 34, 47, 51, 56, 57, 57, 65, 68, 68, 69, 70, 71, 73, 74, 74, 77, 77, 79, 82, 82, 86, 87, 87, 88, 89, 90, 91, 92], "n": 29, "k": 20}}, {"idx": 1, "outputs": -1, "inputs": {"arr": [24, 62, -32, -28, 42, -46, -96, -70, -68, 60, 44, 34, -30, 96, -26, 92, 62, 42, -46, -38, 44, 54, -94, 52, 66, 68, -96, -58, 84, -2, 66, 30, 42], "n": 23, "k": 31}}, {"idx": 2, "outputs": 1, "inputs": {"arr": [0, 0, 0, 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, 1, 1], "n": 32, "k": 31}}, {"idx": 3, "outputs": 0, "inputs": {"arr": [94, 6, 48, 34, 31], "n": 4, "k": 3}}, {"idx": 4, "outputs": 15, "inputs": {"arr": [-98, -88, -82, -80, -76, -70, -70, -60, -60, -52, -50, -46, -44, -44, -44, -20, -18, -12, -12, -12, -10, -8, -6, -4, 4, 4, 18, 28, 32, 34, 42, 42, 44, 46, 48, 54, 60, 70, 70, 72, 78, 78, 78, 78, 84, 86, 90, 96, 98], "n": 45, "k": 30}}, {"idx": 5, "outputs": 4, "inputs": {"arr": [0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0], "n": 15, "k": 8}}, {"idx": 6, "outputs": -1, "inputs": {"arr": [1, 5, 13, 17, 26, 26, 32, 35, 38, 38, 39, 45, 52, 58, 60, 61, 62, 63, 82, 83, 85, 89, 89, 91], "n": 13, "k": 22}}, {"idx": 7, "outputs": 4, "inputs": {"arr": [-68, -52, 4, -90, 90, 88, 38, -18, 86, 4, 14, -32, -14, -44, -88, -50, -12, -26, -68, -20, -30, 22, 0, 14, -40, 58, -6, 28, -44, 8, 28, 96, -46, -12], "n": 32, "k": 22}}, {"idx": 8, "outputs": 2, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 13, "k": 11}}, {"idx": 9, "outputs": -1, "inputs": {"arr": [17, 33, 36, 34, 32, 10, 37, 48, 47, 32, 21, 18, 75, 8, 18, 52, 21, 73, 25, 25, 80, 32, 10, 24, 1, 89, 7, 42, 86, 85, 73, 12, 20, 20, 1, 74, 77, 4, 24, 74, 8], "n": 20, "k": 28}}]
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, k: Int, expected:Int):String {
      try{
        if (validateSolution(PLACEHOLDER_FN_NAME(arr, n, k), expected)) {
                return "PASSED"
        }
        return "FAILED"
      } catch (e: Exception){
        return e::class.simpleName as String
      }
  
  }
  
  fun main() {
      var result: String = ""
      result = driver(arrayListOf(2, 4, 6, 19, 21, 23, 32, 34, 47, 51, 56, 57, 57, 65, 68, 68, 69, 70, 71, 73, 74, 74, 77, 77, 79, 82, 82, 86, 87, 87, 88, 89, 90, 91, 92), 29, 20, 9)
      println("TEST-0...${result}")
      
      result = driver(arrayListOf(24, 62, -32, -28, 42, -46, -96, -70, -68, 60, 44, 34, -30, 96, -26, 92, 62, 42, -46, -38, 44, 54, -94, 52, 66, 68, -96, -58, 84, -2, 66, 30, 42), 23, 31, -1)
      println("TEST-1...${result}")
      
      result = driver(arrayListOf(0, 0, 0, 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, 1, 1), 32, 31, 1)
      println("TEST-2...${result}")
      
      result = driver(arrayListOf(94, 6, 48, 34, 31), 4, 3, 0)
      println("TEST-3...${result}")
      
      result = driver(arrayListOf(-98, -88, -82, -80, -76, -70, -70, -60, -60, -52, -50, -46, -44, -44, -44, -20, -18, -12, -12, -12, -10, -8, -6, -4, 4, 4, 18, 28, 32, 34, 42, 42, 44, 46, 48, 54, 60, 70, 70, 72, 78, 78, 78, 78, 84, 86, 90, 96, 98), 45, 30, 15)
      println("TEST-4...${result}")
      
      result = driver(arrayListOf(0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0), 15, 8, 4)
      println("TEST-5...${result}")
      
      result = driver(arrayListOf(1, 5, 13, 17, 26, 26, 32, 35, 38, 38, 39, 45, 52, 58, 60, 61, 62, 63, 82, 83, 85, 89, 89, 91), 13, 22, -1)
      println("TEST-6...${result}")
      
      result = driver(arrayListOf(-68, -52, 4, -90, 90, 88, 38, -18, 86, 4, 14, -32, -14, -44, -88, -50, -12, -26, -68, -20, -30, 22, 0, 14, -40, 58, -6, 28, -44, 8, 28, 96, -46, -12), 32, 22, 4)
      println("TEST-7...${result}")
      
      result = driver(arrayListOf(0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1), 13, 11, 2)
      println("TEST-8...${result}")
      
      result = driver(arrayListOf(17, 33, 36, 34, 32, 10, 37, 48, 47, 32, 21, 18, 75, 8, 18, 52, 21, 73, 25, 25, 80, 32, 10, 24, 1, 89, 7, 42, 86, 85, 73, 12, 20, 20, 1, 74, 77, 4, 24, 74, 8), 20, 28, -1)
      println("TEST-9...${result}")
      
          
  }
  
  main()
question_info.entry_fn_name: findMaximumAverageSubarrayOfKLength
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