package leetcode;

/**
 * https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/
 */
public class Problem1984 {
    public int minimumDifference(int[] nums, int k) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1984 prob = new Problem1984();
        System.out.println(prob.minimumDifference(new int[]{90}, 1)); // 0
        System.out.println(prob.minimumDifference(new int[]{9,4,1,7}, 2)); // 2
        System.out.println(prob.minimumDifference(new int[]{9,4,1,7}, 3)); // 5
    }
}

package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/
 */
public class Problem1984 {
    public int minimumDifference(int[] nums, int k) {
        int answer = 0;
        Arrays.sort(nums);
        for (int i = nums.length - 1; i - k + 1 >= 0; i--) {
            if (i == nums.length - 1) {
                answer = nums[i] - nums[i - k + 1];
            } else {
                answer = Math.min(answer, nums[i] - nums[i - k + 1]);
            }
        }
        return answer;
    }
}

Solve problem 1984