Choose the smallest possible range of puzzle pieces so that every student gets one piece and the maximum difference is minimized.
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.
Return the minimum possible value of:
among all groups of exactly pieces.
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
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.