> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Difference Between Highest and Lowest

> Tested Python solution for LeetCode 1984 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1984, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Sliding Window](/catalog/topics/sliding-window), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1984   # by problem number
lcpy gen -s minimum_difference_between_highest_and_lowest_of_k_scores   # by problem name
```

## Problem

\<p>You are given a \<strong>0-indexed\</strong> integer array \<code>nums\</code>, where \<code>nums\[i]\</code> represents the score of the \<code>i\<sup>th\</sup>\</code> student. You are also given an integer \<code>k\</code>.\</p>

\<p>Pick the scores of any \<code>k\</code> students from the array so that the \<strong>difference\</strong> between the \<strong>highest\</strong> and the \<strong>lowest\</strong> of the \<code>k\</code> scores is \<strong>minimized\</strong>.\</p>

\<p>Return \<em>the \<strong>minimum\</strong> possible difference\</em>.\</p>

### Examples

```
Input: nums = [90], k = 1
Output: 0
Explanation: There is one way to pick score(s) of one student:
- [90]. The difference between the highest and lowest score is 90 - 90 = 0.
The minimum possible difference is 0.
```

```
Input: nums = [9,4,1,7], k = 2
Output: 2
Explanation: There are six ways to pick score(s) of two students:
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 4 = 5.
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 1 = 8.
- [9,4,1,7]. The difference between the highest and lowest score is 9 - 7 = 2.
- [9,4,1,7]. The difference between the highest and lowest score is 4 - 1 = 3.
- [9,4,1,7]. The difference between the highest and lowest score is 7 - 4 = 3.
- [9,4,1,7]. The difference between the highest and lowest score is 7 - 1 = 6.
The minimum possible difference is 2.
```

### Constraints

* 1 \<= k \<= nums.length \<= 1000
* 0 \<= nums\[i] \<= 10^5

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_difference_between_highest_and_lowest_of_k_scores/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_difference_between_highest_and_lowest_of_k_scores/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n) for the sort
    def minimum_difference(self, nums: list[int], k: int) -> int:
        scores = sorted(nums)
        return min(scores[i + k - 1] - scores[i] for i in range(len(scores) - k + 1))
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) for the sort |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.