title: Minimize The Maximum Difference Between The Heights
signature: minimize_the_maximum_difference_between_the_heights <- function(arr, n, k) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(arr, n, k, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(arr, n, k), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(31L, 33L, 40L, 43L, 44L, 51L, 52L, 56L, 60L, 64L, 66L, 79L, 91L, 93L, 99L), 10L, 13L, 
          19L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(-86L, -62L, -16L, 34L, 54L), 3L, 4L, 
          62L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 29L, 22L, 
          1L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(3L, 5L, 5L, 7L, 9L, 11L, 16L, 17L, 21L, 27L, 36L, 38L, 41L, 44L, 45L, 45L, 48L, 51L, 55L, 57L, 58L, 59L, 59L, 60L, 67L, 67L, 67L, 68L, 70L, 74L, 77L, 84L, 86L, 88L, 90L, 90L, 91L, 91L, 92L, 99L), 28L, 28L, 
          52L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-88L, -84L, -78L, -72L, -66L, -66L, -58L, -56L, -56L, -56L, -54L, -50L, -50L, -38L, -24L, -18L, -12L, -10L, -8L, -4L, 0L, 6L, 8L, 10L, 14L, 40L, 44L, 50L, 58L, 64L, 64L, 68L), 22L, 28L, 
          44L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L), 10L, 8L, 
          1L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(6L, 11L, 13L, 14L, 22L, 25L, 26L, 28L, 30L, 31L, 37L, 37L, 55L, 57L, 61L, 62L, 64L, 67L, 69L, 70L, 71L, 73L, 73L, 75L, 80L, 81L, 82L, 88L, 90L, 98L), 29L, 26L, 
          35L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-90L, -86L, -80L, -78L, -70L, -64L, -62L, -38L, -36L, -32L, -22L, -18L, -10L, 4L, 6L, 20L, 20L, 24L, 44L, 52L, 66L, 82L, 88L, 92L, 98L), 23L, 18L, 
          142L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 20L, 19L, 
          1L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(2L), 0L, 0L, 
          0L
      )))
  
  }
  
  main()
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 );
  }