Choose the smallest possible range of puzzle pieces so that every student gets one piece and the maximum difference is minimized.
Problem
You are given the sizes of puzzle pieces and need to distribute exactly pieces to students.
Each student must receive one puzzle piece. The unfairness of a distribution is the difference between the largest and the smallest piece chosen.
Your task is to choose pieces so that this unfairness is as small as possible.
Goal
Return the minimum possible value of:
among all groups of exactly pieces.
Input Format
- The first line contains two integers and .
- The second line contains integers describing the sizes of the puzzle pieces.
Output Format
- Print one integer: the minimum possible unfairness.
Constraints
- Piece sizes are integers.
- A valid selection always exists.
Hints
- Sort the pieces first.
- After sorting, consider every contiguous block of length and compute its range.
Constraints
- Select exactly pieces.
- Minimize the difference between the largest and smallest selected piece.
- Sorting is allowed and typically essential.
Example 1
Input
7 3 10 100 300 200 1000 20 30
Output
20
Explanation
After sorting: 10 20 30 100 200 300 1000. The best group of 3 consecutive pieces is 10, 20, 30, giving unfairness 30 - 10 = 20.
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.