Max Min Discussions | | HackerRank

Max Min

  • + 0 comments

    Java solution:

        public static int maxMin(int k, List<Integer> arr) {
        // Write your code here
            arr.sort(Comparator.naturalOrder());
            int result = Integer.MAX_VALUE;
            for (int i = 0; i <= arr.size() - k; i++) {
                result = Math.min(result, arr.get(i + k - 1) - arr.get(i)); 
            }
            return result;
        }