title: K Numbers Difference Maximum Minimum K Number Minimized
signature: class Solution {
  
      public Long kNumbersDifferenceMaximumMinimumKNumberMinimized(ArrayList<Integer> arr, Integer n, Integer k) {
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, Long 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(1, 1, 2, 7, 8, 14, 16, 20, 20, 23, 24, 24, 29, 30, 32, 34, 35, 37, 38, 43, 44, 46, 50, 52, 55, 57, 61, 71, 79, 86, 86, 89, 91, 91, 95)), 33, 17, 32L);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-80, -74, -70, -68, -66, -66, -62, -60, -60, -52, -50, -46, -34, -34, -28, -18, -4, 0, 6, 8, 16, 34, 34, 34, 42, 42, 44, 46, 54, 54, 54, 58, 62, 68, 72, 76, 78, 80, 84, 88, 90, 90)), 33, 33, 142L);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1)), 8, 13, 2147483647L);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(32)), 0, 0, 0L);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(-96, -78, -76, -72, -72, -70, -54, -46, -40, -34, -30, -26, -24, -22, -18, -16, -6, -4, -4, 2, 6, 14, 16, 18, 30, 30, 36, 54, 54, 58, 66, 72, 78, 80, 80, 82, 88, 98)), 26, 25, 108L);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1)), 39, 41, 2147483647L);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(3, 13, 14, 18, 23, 32, 67, 72, 75, 76, 87, 95)), 10, 8, 62L);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(8, 30)), 1, 1, 0L);
          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, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), 27, 33, 2147483647L);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution(new ArrayList<>(Arrays.asList(2, 3, 3, 7, 9, 15, 16, 19, 21, 25, 27, 29, 30, 31, 33, 35, 36, 36, 37, 40, 41, 42, 52, 52, 53, 57, 57, 63, 67, 67, 69, 72, 73, 74, 78, 83, 86, 87, 93, 94, 95, 95)), 36, 37, 2147483647L);
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(ArrayList<Integer> arr, Integer n, Integer k, Long expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(arr, n, k),expected);
      }
      
      public boolean validateSolution(Long actual, Long expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
source_py: def k_numbers_difference_maximum_minimum_k_number_minimized(arr, n, k):
      result = (+ 2147483647)
      arr.sort()
      for i in range(((n - k) + 1)):
          result = int(min(result, (arr[((i + k) - 1)] - arr[i])))
      return result
source_cpp: int k_numbers_difference_maximum_minimum_k_number_minimized(vector<int> arr, int n, int k) {
    int result = INT_MAX;
    sort(arr.begin(), arr.end());
    for ( int i = 0;
    i <= n - k;
    i ++ ) result = min ( result, arr [ i + k - 1 ] - arr [ i ] );
    return result;
  }