Show problems as
Classic problem #1984EasyArraySliding WindowSorting

Minimum Difference Between Highest and Lowest of K Scores

Real-world version: The Tightest Choice of k Readings
Tracked in this browser only
Read Minimum Difference Between Highest and Lowest of K Scores on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A rack holds the readings readings. Choose exactly k of them, and let the spread of a choice be its largest reading less its smallest.

Return the smallest spread any choice of k readings can manage.

Examples

Example 1

Input
readings = [1, 5, 6, 14, 15], k = 3
Output
5

In order the readings run 1, 5, 6, 14, 15. The three standing closest together are 1, 5 and 6, spreading 5.

Example 2

Input
readings = [5, 5, 5], k = 2
Output
0

Every reading is the same, so any two of them spread nothing at all.

Example 3

Input
readings = [1, 100000], k = 2
Output
99999

Both readings have to be taken, so the spread is the gap between them.

Constraints

  • 1 <= k <= readings.length <= 1000
  • 0 <= readings[i] <= 10^5

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def minimum_difference(readings: list[int], k: int) -> int:
Java
public int minimumDifference(int[] readings, int k)