Max Min Discussions | | HackerRank

Max Min

  • + 0 comments

    Javascript

    function maxMin(k: number, arr: number[]): number {
      let smallest = Number.POSITIVE_INFINITY;
      const sorted = arr.sort((a,b) => a-b);
      
      for (let i=0; i <= sorted.length - k; i++) {
        smallest = Math.min(smallest, sorted[i + k - 1] - sorted[i]);
      }
      
      return smallest;
    }