title: Maximum Number Chocolates Distributed Equally Among K Students
signature: class Solution {
  
      public Integer maximumNumberChocolatesDistributedEquallyAmongKStudents(ArrayList<Integer> arr, Integer n, Integer k) {
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 );
  }
question_info.extension: java
question_info.entry_fn_name: maximumNumberChocolatesDistributedEquallyAmongKStudents
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 29, "inputs": {"arr": [2, 3, 8, 8, 12, 14, 23, 25, 25, 27, 27, 29, 40, 42, 49, 52, 52, 54, 56, 57, 61, 68, 74, 77, 81, 82, 83, 84, 85, 85, 85, 87, 87, 88, 88, 90, 92, 96, 96], "n": 27, "k": 32}}, {"idx": 1, "outputs": 0, "inputs": {"arr": [-90, -34, 26, -20, -12, -42, 28, 12, -6, 58, -46, 4, -30, -28, -14], "n": 8, "k": 14}}, {"idx": 2, "outputs": 0, "inputs": {"arr": [0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], "n": 7, "k": 9}}, {"idx": 3, "outputs": 48, "inputs": {"arr": [58, 81, 36, 2, 51, 71, 77, 34, 91, 82, 47, 4, 91, 83, 26, 96, 3, 6], "n": 16, "k": 12}}, {"idx": 4, "outputs": 2, "inputs": {"arr": [-92, -86, -82, -68, -60, -46, -40, -28, -26, -24, -2, -2, 2, 2, 10, 10, 10, 16, 16, 18, 20, 20, 22, 30, 34, 38, 56, 56, 60, 62, 62, 68, 82, 94, 94, 98], "n": 20, "k": 31}}, {"idx": 5, "outputs": 0, "inputs": {"arr": [0, 1, 0], "n": 2, "k": 2}}, {"idx": 6, "outputs": 36, "inputs": {"arr": [3, 8, 15, 19, 21, 26, 28, 31, 31, 42, 45, 48, 57, 75, 75, 78, 79, 85, 91, 99], "n": 17, "k": 16}}, {"idx": 7, "outputs": 6, "inputs": {"arr": [-38, 42, 40, -60, -16, -36, 44, 60, -86, -38, 30, -22, -30, -96, -66], "n": 12, "k": 10}}, {"idx": 8, "outputs": 0, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], "n": 13, "k": 16}}, {"idx": 9, "outputs": 31, "inputs": {"arr": [95, 38, 91, 75, 43, 95, 23, 36, 51, 4, 38, 53, 52, 58, 55, 3, 19, 22, 84, 9], "n": 14, "k": 19}}]
question_info.test_code: import static java.util.Map.entry;  
  import java.util.*;
  import java.lang.*;
  // TESTING CODE 
  // ============================================
  class QuestionEvaluator {
      public static String validateSolution(ArrayList<Integer> arr, Integer n, Integer k, Integer expected){
          try{
              Driver driver = new Driver();
              if (driver.run(arr, n, k,expected)){
                  return "PASSED";
              }
              return "FAILED";
              
          } catch (Exception exception_obj){
              return exception_obj.getClass().getSimpleName();
          }
  
      }
      public static void main(String[] args) {
          String result;
          result = validateSolution(new ArrayList<>(Arrays.asList(2, 3, 8, 8, 12, 14, 23, 25, 25, 27, 27, 29, 40, 42, 49, 52, 52, 54, 56, 57, 61, 68, 74, 77, 81, 82, 83, 84, 85, 85, 85, 87, 87, 88, 88, 90, 92, 96, 96)), 27, 32, 29);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-90, -34, 26, -20, -12, -42, 28, 12, -6, 58, -46, 4, -30, -28, -14)), 8, 14, 0);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 7, 9, 0);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(58, 81, 36, 2, 51, 71, 77, 34, 91, 82, 47, 4, 91, 83, 26, 96, 3, 6)), 16, 12, 48);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-92, -86, -82, -68, -60, -46, -40, -28, -26, -24, -2, -2, 2, 2, 10, 10, 10, 16, 16, 18, 20, 20, 22, 30, 34, 38, 56, 56, 60, 62, 62, 68, 82, 94, 94, 98)), 20, 31, 2);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 1, 0)), 2, 2, 0);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(3, 8, 15, 19, 21, 26, 28, 31, 31, 42, 45, 48, 57, 75, 75, 78, 79, 85, 91, 99)), 17, 16, 36);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-38, 42, 40, -60, -16, -36, 44, 60, -86, -38, 30, -22, -30, -96, -66)), 12, 10, 6);
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1)), 13, 16, 0);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(95, 38, 91, 75, 43, 95, 23, 36, 51, 4, 38, 53, 52, 58, 55, 3, 19, 22, 84, 9)), 14, 19, 31);
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(ArrayList<Integer> arr, Integer n, Integer k, Integer expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(arr, n, k),expected);
      }
      
      public boolean validateSolution(Integer actual, Integer expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: java __FILENAME__