Ctrl K

Deal Into Consecutive Sets

Medium
There are cards on the table, each with a number: cards[i] is the number on card i. Several cards may show the same number. You need to split all the cards into sets of exactly size cards so that each set holds consecutive numbers: x, x + 1, …, x + size - 1. Every card must go into exactly one set. Return True if such a split is possible and False otherwise.
Example 1
cards=[5, 3, 4, 4, 6, 5], size=3 → True

The cards split into the sets [3, 4, 5] and [4, 5, 6].

Example 2
cards=[1, 2, 4, 5], size=2 → True

[1, 2] and [4, 5] work: each set holds consecutive values.

Example 3
cards=[2, 3, 3, 4], size=2 → True

The 2 can only go with a 3, which leaves 3 and 4, also a valid set. The answer is True.

Constraints
  • 1 ≤ len(cards) ≤ 10⁵
  • 0 ≤ cards[i] ≤ 10⁹
  • 1 ≤ size ≤ len(cards)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsJobs With a Cooldown MediumPair Each Number With Its Double MediumSpread Out the Labels MediumPick Within Tag Limits MediumSplit Into Straight Runs Medium
loading Python…
Input
cards=[5, 3, 4, 4, 6, 5], size=3
Expected
True

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