title: Find Maximum Average Subarray Of K Length
signature: find_maximum_average_subarray_of_k_length <- function(arr, n, k) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, k, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n, k), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(2L, 4L, 6L, 19L, 21L, 23L, 32L, 34L, 47L, 51L, 56L, 57L, 57L, 65L, 68L, 68L, 69L, 70L, 71L, 73L, 74L, 74L, 77L, 77L, 79L, 82L, 82L, 86L, 87L, 87L, 88L, 89L, 90L, 91L, 92L), 29L, 20L, 
          9L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(24L, 62L, -32L, -28L, 42L, -46L, -96L, -70L, -68L, 60L, 44L, 34L, -30L, 96L, -26L, 92L, 62L, 42L, -46L, -38L, 44L, 54L, -94L, 52L, 66L, 68L, -96L, -58L, 84L, -2L, 66L, 30L, 42L), 23L, 31L, 
          -1L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 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, 1L, 1L), 32L, 31L, 
          1L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(94L, 6L, 48L, 34L, 31L), 4L, 3L, 
          0L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-98L, -88L, -82L, -80L, -76L, -70L, -70L, -60L, -60L, -52L, -50L, -46L, -44L, -44L, -44L, -20L, -18L, -12L, -12L, -12L, -10L, -8L, -6L, -4L, 4L, 4L, 18L, 28L, 32L, 34L, 42L, 42L, 44L, 46L, 48L, 54L, 60L, 70L, 70L, 72L, 78L, 78L, 78L, 78L, 84L, 86L, 90L, 96L, 98L), 45L, 30L, 
          15L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L), 15L, 8L, 
          4L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(1L, 5L, 13L, 17L, 26L, 26L, 32L, 35L, 38L, 38L, 39L, 45L, 52L, 58L, 60L, 61L, 62L, 63L, 82L, 83L, 85L, 89L, 89L, 91L), 13L, 22L, 
          -1L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-68L, -52L, 4L, -90L, 90L, 88L, 38L, -18L, 86L, 4L, 14L, -32L, -14L, -44L, -88L, -50L, -12L, -26L, -68L, -20L, -30L, 22L, 0L, 14L, -40L, 58L, -6L, 28L, -44L, 8L, 28L, 96L, -46L, -12L), 32L, 22L, 
          4L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 13L, 11L, 
          2L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(17L, 33L, 36L, 34L, 32L, 10L, 37L, 48L, 47L, 32L, 21L, 18L, 75L, 8L, 18L, 52L, 21L, 73L, 25L, 25L, 80L, 32L, 10L, 24L, 1L, 89L, 7L, 42L, 86L, 85L, 73L, 12L, 20L, 20L, 1L, 74L, 77L, 4L, 24L, 74L, 8L), 20L, 28L, 
          -1L
      )))
  
  }
  
  main()
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;
  }