Ctrl K

Most Even Pairings

Medium
A chess club is running a night of blitz games. The players' ratings are given in the array ratings, and exactly matches games must be played. Each game pairs two different players, and nobody plays more than one game. Some players may sit out. The gap of a game is the absolute difference between its two players' ratings. The organizer wants even the most lopsided game to be as close as possible. Return the smallest possible value of the largest gap across all matches games. If matches is 0, the answer is 0.
Example 1
ratings=[15, 4, 9, 12, 5, 20], matches=2 → 3

Pair 4 with 5 (gap 1) and 9 with 12 (gap 3). The worse of the two gaps is 3, and nothing beats that.

Example 2
ratings=[8, 3, 8], matches=1 → 0

Match the two players rated 8: the gap is 0.

Example 3
ratings=[1, 100], matches=0 → 0

No matches are needed, and the worst gap over an empty set counts as 0.

Constraints
  • 1 ≤ len(ratings) ≤ 10⁵
  • 0 ≤ ratings[i] ≤ 10⁹
  • 0 ≤ matches ≤ len(ratings) / 2 (rounded down)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsStick Triples That Form a Triangle MediumBest-Paid Gig for Each Worker MediumA Plan for Draining Ponds MediumPrices That Fade With Each Sale MediumEven Out With Increments Medium
loading Python…
Input
ratings=[15, 4, 9, 12, 5, 20], matches=2
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 103 tests on the server, hidden ones included.