title: Minimize The Maximum Difference Between The Heights
signature: minimizeTheMaximumDifferenceBetweenTheHeights :: [Integer] -> Integer -> Integer -> Integer
  minimizeTheMaximumDifferenceBetweenTheHeights arr n k = 
test_code: import Text.Printf
  import Control.Exception (catch, SomeException)
  import Data.Typeable
  import qualified Data.List as List
  import qualified Data.Map as Map
  import qualified Data.Set as Set
  
  -- SOLUTION CODE
  -- ============================================
  
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  validateSolution :: Integer -> Integer -> Bool
  validateSolution actual expected = do
      actual == expected
  
  
  driver :: [Integer] -> Integer -> Integer -> Integer -> String
  driver arr n k expected = do
      if (validateSolution (PLACEHOLDER_FN_NAME arr n k) expected)
          then "PASSED"
      else "FAILED"
  
  main = do
      printf "TEST-0...%s\n" (driver ([31, 33, 40, 43, 44, 51, 52, 56, 60, 64, 66, 79, 91, 93, 99]) (10) (13) (19) )
  
      printf "TEST-1...%s\n" (driver ([-86, -62, -16, 34, 54]) (3) (4) (62) )
  
      printf "TEST-2...%s\n" (driver ([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) )
  
      printf "TEST-3...%s\n" (driver ([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) )
  
      printf "TEST-4...%s\n" (driver ([-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) )
  
      printf "TEST-5...%s\n" (driver ([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]) (10) (8) (1) )
  
      printf "TEST-6...%s\n" (driver ([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) )
  
      printf "TEST-7...%s\n" (driver ([-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) )
  
      printf "TEST-8...%s\n" (driver ([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) )
  
      printf "TEST-9...%s\n" (driver ([2]) (0) (0) (0) )
  
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 );
  }