title: Maximum Number Chocolates Distributed Equally Among K Students
signature: maximum_number_chocolates_distributed_equally_among_k_students <- 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, 3L, 8L, 8L, 12L, 14L, 23L, 25L, 25L, 27L, 27L, 29L, 40L, 42L, 49L, 52L, 52L, 54L, 56L, 57L, 61L, 68L, 74L, 77L, 81L, 82L, 83L, 84L, 85L, 85L, 85L, 87L, 87L, 88L, 88L, 90L, 92L, 96L, 96L), 27L, 32L, 
          29L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-90L, -34L, 26L, -20L, -12L, -42L, 28L, 12L, -6L, 58L, -46L, 4L, -30L, -28L, -14L), 8L, 14L, 
          0L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 7L, 9L, 
          0L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(58L, 81L, 36L, 2L, 51L, 71L, 77L, 34L, 91L, 82L, 47L, 4L, 91L, 83L, 26L, 96L, 3L, 6L), 16L, 12L, 
          48L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-92L, -86L, -82L, -68L, -60L, -46L, -40L, -28L, -26L, -24L, -2L, -2L, 2L, 2L, 10L, 10L, 10L, 16L, 16L, 18L, 20L, 20L, 22L, 30L, 34L, 38L, 56L, 56L, 60L, 62L, 62L, 68L, 82L, 94L, 94L, 98L), 20L, 31L, 
          2L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(0L, 1L, 0L), 2L, 2L, 
          0L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(3L, 8L, 15L, 19L, 21L, 26L, 28L, 31L, 31L, 42L, 45L, 48L, 57L, 75L, 75L, 78L, 79L, 85L, 91L, 99L), 17L, 16L, 
          36L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-38L, 42L, 40L, -60L, -16L, -36L, 44L, 60L, -86L, -38L, 30L, -22L, -30L, -96L, -66L), 12L, 10L, 
          6L
      )))
  
      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), 13L, 16L, 
          0L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(95L, 38L, 91L, 75L, 43L, 95L, 23L, 36L, 51L, 4L, 38L, 53L, 52L, 58L, 55L, 3L, 19L, 22L, 84L, 9L), 14L, 19L, 
          31L
      )))
  
  }
  
  main()
source_py: def maximum_number_chocolates_distributed_equally_among_k_students(arr, n, k):
      (um, curr_rem, maxSum) = ({
          
      }, 0, 0)
      sm = ([0] * n)
      sm[0] = arr[0]
      for i in range(1, n):
          sm[i] = (sm[(i - 1)] + arr[i])
      for i in range(n):
          curr_rem = (sm[i] % k)
          if ((not curr_rem) and (maxSum < sm[i])):
              maxSum = sm[i]
          elif (not (curr_rem in um)):
              um[curr_rem] = i
          elif (maxSum < (sm[i] - sm[um[curr_rem]])):
              maxSum = (sm[i] - sm[um[curr_rem]])
      return (maxSum // k)
source_cpp: int maximum_number_chocolates_distributed_equally_among_k_students(vector<int> arr, int n, int k) {
    unordered_map < int, int > um;
    int sum [ n ], curr_rem;
    int maxSum = 0;
    sum [ 0 ] = arr [ 0 ];
    for ( int i = 1;
    i < n;
    i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i ];
    for ( int i = 0;
    i < n;
    i ++ ) {
      curr_rem = sum [ i ] % k;
      if ( curr_rem == 0 ) {
        if ( maxSum < sum [ i ] ) maxSum = sum [ i ];
      }
      else if ( um . find ( curr_rem ) == um . end ( ) ) um [ curr_rem ] = i;
      else if ( maxSum < ( sum [ i ] - sum [ um [ curr_rem ] ] ) ) maxSum = sum [ i ] - sum [ um [ curr_rem ] ];
    }
    return ( maxSum / k );
  }