title: K Numbers Difference Maximum Minimum K Number Minimized
signature: long long kNumbersDifferenceMaximumMinimumKNumberMinimized(vector<int> arr, int n, int k) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(long long actual, long long expected){
      return actual == expected;
  }
  
  string driver(vector<int> arr, int n, int k, long long expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(arr, n, k), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver({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, 32); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver({-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, 142); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver({0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, 8, 13, 2147483647); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver({32}, 0, 0, 0); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver({-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, 108); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver({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, 2147483647); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver({3, 13, 14, 18, 23, 32, 67, 72, 75, 76, 87, 95}, 10, 8, 62); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver({8, 30}, 1, 1, 0); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver({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, 2147483647); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver({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, 2147483647); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
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;
  }