title: JS How can I increase the performance of this Max/Min coding challenge?
body: <p>I'm trying to solve some 'hackerrank.com' coding challenges.
  I'm stuck on this one:</p>
  <p>You will be given an array of integers <code>arr</code> and a single integer <code>k</code>. You must create an array <code>arr'</code> of length <code>k</code> from elements of <code>arr</code> such that its unfairness is minimized.
  Unfairness is defined as <code>max(arr') - min(arr')</code>
  The function should return the <code>minimum possible unfairness</code></p>
  <p>My code works fine for the majority of test cases. However, in three of those test cases - the ones where the size of <code>arr</code> and <code>k</code> is particularly huge - fail due to an excession of the given time limit.</p>
  <p>How can I optimize the performance of my code?
  Thanks in advance</p>
  <pre><code>function maxMin(k, arr) {
      // create array to push unfairness values to
      var unfairnesses = [];
      // sort given array in ascending order
      arr.sort(function(a, b) {
          return a - b;
      });
      // loop over sorted array    
      for(var i = 0; i &lt; arr.length - k + 1; i++) {
          // get array with the length of k at position i
          var tempArr = arr.slice(i, i + k);
          // determine Max and Min of the sliced array
          var tempArrMax = Math.max(...tempArr);
          var tempArrMin = Math.min(...tempArr);
          // get unfairness of the sliced array
          var thisUnfairness = tempArrMax - tempArrMin;
          // push sliced-array-unfairness to unfairnesses array
          unfairnesses.push(thisUnfairness);
      }
  
      // return minimal value of unfairnesses array
      return Math.min(...unfairnesses);
  }
  </code></pre>