title: Chocolate Distribution Problem
signature: chocolate_distribution_problem <- function(arr, n, m) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, m, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n, m), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(2L, 5L, 11L, 23L, 33L, 35L, 39L, 51L, 52L, 56L, 74L, 76L, 76L, 79L, 85L, 88L, 93L, 98L), 16L, 13L, 
          65L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-94L, -94L, -88L, -84L, -74L, -70L, -56L, -50L, -48L, -42L, -34L, -24L, -18L, -14L, 0L, 4L, 4L, 16L, 16L, 30L, 36L, 42L, 52L, 56L, 72L, 74L, 76L, 84L, 88L), 15L, 28L, 
          -1L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 1L, 1L, 1L, 1L), 5L, 5L, 
          1L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(3L, 4L, 5L, 5L, 12L, 16L, 16L, 16L, 18L, 29L, 37L, 38L, 40L, 43L, 44L, 49L, 57L, 60L, 62L, 63L, 67L, 69L, 73L, 74L, 75L, 76L, 78L, 84L, 85L, 86L, 88L, 89L, 92L), 25L, 18L, 
          57L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-98L, -80L, -50L, -44L, -42L, -36L, -36L, -28L, -10L, -8L, -4L, -2L, 2L, 10L, 18L, 18L, 26L, 32L, 36L, 56L, 80L, 86L, 88L, 90L), 16L, 12L, 
          60L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 13L, 14L, 
          -1L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(13L, 15L, 62L, 65L, 87L), 3L, 4L, 
          -1L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-92L, -88L, -88L, -80L, -76L, -62L, -60L, -60L, -50L, -48L, -36L, -36L, -32L, -26L, -14L, -8L, 4L, 18L, 18L, 22L, 28L, 30L, 30L, 32L, 48L, 48L, 58L, 60L, 64L, 72L, 78L, 82L, 94L), 31L, 17L, 
          90L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 9L, 6L, 
          1L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(17L, 23L, 25L, 38L, 40L, 53L, 58L, 73L, 77L), 8L, 6L, 
          35L
      )))
  
  }
  
  main()
source_py: def chocolate_distribution_problem(arr, n, m):
      if ((m == 0) or (n == 0)):
          return 0
      arr.sort()
      if (n < m):
          return (- 1)
      min_diff = sys.maxsize
      first = 0
      last = 0
      i = 0
      while (((i + m) - 1) < n):
          diff = (arr[((i + m) - 1)] - arr[i])
          if (diff < min_diff):
              min_diff = diff
              first = i
              last = ((i + m) - 1)
          i += 1
      return (arr[last] - arr[first])
source_cpp: int chocolate_distribution_problem(vector<int> arr, int n, int m) {
    if ( m == 0 || n == 0 ) return 0;
    sort(arr.begin(), arr.end());
    if ( n < m ) return - 1;
    int min_diff = INT_MAX;
    int first = 0, last = 0;
    for ( int i = 0;
    i + m - 1 < n;
    i ++ ) {
      int diff = arr [ i + m - 1 ] - arr [ i ];
      if ( diff < min_diff ) {
        min_diff = diff;
        first = i;
        last = i + m - 1;
      }
    }
    return ( arr [ last ] - arr [ first ] );
  }