All problems
1176EasyArraySliding WindowSorting

The Tightest Choice of k Readings

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1984Minimum Difference Between Highest and Lowest of K Scores

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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