title: Minimize The Maximum Difference Between The Heights
signature: class Solution {
  
      public int MinimizeTheMaximumDifferenceBetweenTheHeights(List<int> arr, int n, int k) {
test_code: using System;
  using System.Numerics;
  using System.Diagnostics;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Security.Cryptography;
  using System.Web.UI;
  using System.Web.Script.Serialization;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  namespace EvaluatePrediction {
      class TestRunner {
          public bool ValidateSolution(int actual, int expected){
              return new JavaScriptSerializer().Serialize(actual) == new JavaScriptSerializer().Serialize(expected);
          }
  
          public string driver(List<int> arr, int n, int k, int expected){
              try{
                PLACEHOLDER_CLS_NAME solution_cls = new PLACEHOLDER_CLS_NAME();
                int pred_result = solution_cls.PLACEHOLDER_FN_NAME(arr, n, k); 
                if (ValidateSolution(
                      pred_result,
                      expected)){
                  return "PASSED";
                }
                return "FAILED";
              } 
              catch (Exception exception_obj)
              {
                return exception_obj.GetType().FullName;
              }
          }
          static void Main(string[] args) {
              TestRunner runner = new TestRunner();
              String result;
              result = runner.driver(
                  new List<int>{31, 33, 40, 43, 44, 51, 52, 56, 60, 64, 66, 79, 91, 93, 99}, 10, 13, 
                  19
              );
              Console.WriteLine(string.Format(
                  "TEST-0...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{-86, -62, -16, 34, 54}, 3, 4, 
                  62
              );
              Console.WriteLine(string.Format(
                  "TEST-1...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{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
              );
              Console.WriteLine(string.Format(
                  "TEST-2...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{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
              );
              Console.WriteLine(string.Format(
                  "TEST-3...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{-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
              );
              Console.WriteLine(string.Format(
                  "TEST-4...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1}, 10, 8, 
                  1
              );
              Console.WriteLine(string.Format(
                  "TEST-5...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{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
              );
              Console.WriteLine(string.Format(
                  "TEST-6...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{-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
              );
              Console.WriteLine(string.Format(
                  "TEST-7...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{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
              );
              Console.WriteLine(string.Format(
                  "TEST-8...{0}",
                  result)
              );result = runner.driver(
                  new List<int>{2}, 0, 0, 
                  0
              );
              Console.WriteLine(string.Format(
                  "TEST-9...{0}",
                  result)
              );
          }
      }
  }
  
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 );
  }