Skip to main content
Back to problems
Leetcode
Easy
Arrays
Sorting
Minimum Difference Between Highest And Lowest Of K Scores

Choose any group of k scores and minimize the spread between the largest and smallest score in that group.

Acceptance 0%
Problem Statement

Problem

You are given an array of student scores and an integer k. Your task is to select exactly k scores such that the difference between the highest score and the lowest score in the selected group is as small as possible.

Return that minimum possible difference.

A group is any subset of k elements from the array. The selected scores do not need to be contiguous in the original order.

Key idea

The answer depends on how tightly k values can fit together after ordering the scores.

Input Format

  • scores: an array of integers
  • k: an integer representing the number of scores to choose

Output Format

Return a single integer: the minimum possible difference between the maximum and minimum score among any selected group of size k.

Constraints

  • 1 <= k <= scores.length
  • scores.length is at least 1
  • Score values are integers
  • Use a solution efficient enough for typical interview constraints
Examples
Sample cases returned by the problem API.

Example 1

Input

scores = [90, 100, 110, 120, 130], k = 3

Output

20

Explanation

After sorting, the best group is [90, 100, 110] or [100, 110, 120] or [110, 120, 130]. The smallest difference is 20.

Example 2

Input

scores = [9, 4, 1, 7], k = 2

Output

3

Explanation

Sort the scores to get [1, 4, 7, 9]. The best pair is [1, 4] or [4, 7] or [7, 9], each with difference 3.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.