title: Minimize The Maximum Difference Between The Heights
signature: fun minimizeTheMaximumDifferenceBetweenTheHeights(arr: ArrayList<Int>, n: Int, k: Int): Int {
source_py: def minimize_the_maximum_difference_between_the_heights(arr, n, k):
      if (n == 1):
          return 0
      arr.sort()
      ans = (arr[(n - 1)] - arr[0])
      small = (arr[0] + k)
      big = (arr[(n - 1)] - k)
      if (small > big):
          (small, big) = (big, small)
      for i in range(1, (n - 1)):
          subtract = (arr[i] - k)
          add = (arr[i] + k)
          if ((subtract >= small) or (add <= big)):
              continue
          if ((big - subtract) <= (add - small)):
              small = subtract
          else:
              big = add
      return min(ans, (big - small))
source_cpp: int minimize_the_maximum_difference_between_the_heights(vector<int> arr, int n, int k) {
    if ( n == 1 ) return 0;
    sort(arr.begin(), arr.end());
    int ans = arr [ n - 1 ] - arr [ 0 ];
    int small = arr [ 0 ] + k;
    int big = arr [ n - 1 ] - k;
    if ( small > big ) swap ( small, big );
    for ( int i = 1;
    i < n - 1;
    i ++ ) {
      int subtract = arr [ i ] - k;
      int add = arr [ i ] + k;
      if ( subtract >= small || add <= big ) continue;
      if ( big - subtract <= add - small ) small = subtract;
      else big = add;
    }
    return min ( ans, big - small );
  }
question_info.test_list: [{"idx": 0, "outputs": 19, "inputs": {"arr": [31, 33, 40, 43, 44, 51, 52, 56, 60, 64, 66, 79, 91, 93, 99], "n": 10, "k": 13}}, {"idx": 1, "outputs": 62, "inputs": {"arr": [-86, -62, -16, 34, 54], "n": 3, "k": 4}}, {"idx": 2, "outputs": 1, "inputs": {"arr": [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], "n": 29, "k": 22}}, {"idx": 3, "outputs": 52, "inputs": {"arr": [3, 5, 5, 7, 9, 11, 16, 17, 21, 27, 36, 38, 41, 44, 45, 45, 48, 51, 55, 57, 58, 59, 59, 60, 67, 67, 67, 68, 70, 74, 77, 84, 86, 88, 90, 90, 91, 91, 92, 99], "n": 28, "k": 28}}, {"idx": 4, "outputs": 44, "inputs": {"arr": [-88, -84, -78, -72, -66, -66, -58, -56, -56, -56, -54, -50, -50, -38, -24, -18, -12, -10, -8, -4, 0, 6, 8, 10, 14, 40, 44, 50, 58, 64, 64, 68], "n": 22, "k": 28}}, {"idx": 5, "outputs": 1, "inputs": {"arr": [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1], "n": 10, "k": 8}}, {"idx": 6, "outputs": 35, "inputs": {"arr": [6, 11, 13, 14, 22, 25, 26, 28, 30, 31, 37, 37, 55, 57, 61, 62, 64, 67, 69, 70, 71, 73, 73, 75, 80, 81, 82, 88, 90, 98], "n": 29, "k": 26}}, {"idx": 7, "outputs": 142, "inputs": {"arr": [-90, -86, -80, -78, -70, -64, -62, -38, -36, -32, -22, -18, -10, 4, 6, 20, 20, 24, 44, 52, 66, 82, 88, 92, 98], "n": 23, "k": 18}}, {"idx": 8, "outputs": 1, "inputs": {"arr": [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], "n": 20, "k": 19}}, {"idx": 9, "outputs": 0, "inputs": {"arr": [2], "n": 0, "k": 0}}]
question_info.extension: kts
question_info.test_code: package main
  
  import kotlin.text.*
  import kotlin.math.*
  
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  fun validateSolution(actual:Int, expected:Int):Boolean{
    return actual.equals(expected)
  }
  fun driver(arr: ArrayList<Int>, n: Int, k: Int, expected:Int):String {
      try{
        if (validateSolution(PLACEHOLDER_FN_NAME(arr, n, k), expected)) {
                return "PASSED"
        }
        return "FAILED"
      } catch (e: Exception){
        return e::class.simpleName as String
      }
  
  }
  
  fun main() {
      var result: String = ""
      result = driver(arrayListOf(31, 33, 40, 43, 44, 51, 52, 56, 60, 64, 66, 79, 91, 93, 99), 10, 13, 19)
      println("TEST-0...${result}")
      
      result = driver(arrayListOf(-86, -62, -16, 34, 54), 3, 4, 62)
      println("TEST-1...${result}")
      
      result = driver(arrayListOf(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), 29, 22, 1)
      println("TEST-2...${result}")
      
      result = driver(arrayListOf(3, 5, 5, 7, 9, 11, 16, 17, 21, 27, 36, 38, 41, 44, 45, 45, 48, 51, 55, 57, 58, 59, 59, 60, 67, 67, 67, 68, 70, 74, 77, 84, 86, 88, 90, 90, 91, 91, 92, 99), 28, 28, 52)
      println("TEST-3...${result}")
      
      result = driver(arrayListOf(-88, -84, -78, -72, -66, -66, -58, -56, -56, -56, -54, -50, -50, -38, -24, -18, -12, -10, -8, -4, 0, 6, 8, 10, 14, 40, 44, 50, 58, 64, 64, 68), 22, 28, 44)
      println("TEST-4...${result}")
      
      result = driver(arrayListOf(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1), 10, 8, 1)
      println("TEST-5...${result}")
      
      result = driver(arrayListOf(6, 11, 13, 14, 22, 25, 26, 28, 30, 31, 37, 37, 55, 57, 61, 62, 64, 67, 69, 70, 71, 73, 73, 75, 80, 81, 82, 88, 90, 98), 29, 26, 35)
      println("TEST-6...${result}")
      
      result = driver(arrayListOf(-90, -86, -80, -78, -70, -64, -62, -38, -36, -32, -22, -18, -10, 4, 6, 20, 20, 24, 44, 52, 66, 82, 88, 92, 98), 23, 18, 142)
      println("TEST-7...${result}")
      
      result = driver(arrayListOf(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), 20, 19, 1)
      println("TEST-8...${result}")
      
      result = driver(arrayListOf(2), 0, 0, 0)
      println("TEST-9...${result}")
      
          
  }
  
  main()
question_info.entry_fn_name: minimizeTheMaximumDifferenceBetweenTheHeights
question_info.entry_cls_name: Solution
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: kotlinc -script __FILENAME__ -no-reflect -nowarn