Imagine you're a teacher selecting students for a special program based on their test scores. You have a list of student scores and need to choose exactly k students such that the performance gap between the highest and lowest scoring selected students is as small as possible.
Given a 0-indexed integer array nums where nums[i] represents the score of the i-th student, and an integer k, your task is to select any k students from the array to minimize the difference between the highest and lowest scores among your selected students.
Goal: Return the minimum possible difference between the highest and lowest scores when selecting exactly k students.
Example: If scores are [90, 30, 87, 50, 20] and k = 3, selecting students with scores [87, 90, 50] gives a difference of 90 - 50 = 40, but selecting [30, 20, 50] gives 50 - 20 = 30, which is better!
Input & Output
Constraints
- 1 โค k โค nums.length โค 1000
- 0 โค nums[i] โค 105
- k represents the exact number of students to select