Divide Array in Sets of K Consecutive Numbers - Problem
Can you organize a deck of cards into perfect consecutive sequences?

Given an array of integers nums and a positive integer k, your task is to determine whether it's possible to divide this array into groups of exactly k consecutive numbers.

For example, if you have [1, 2, 3, 6, 2, 3, 4, 7, 8] and k = 3, you need to check if you can form groups like [1, 2, 3], [2, 3, 4], and [6, 7, 8] using all numbers exactly once.

Goal: Return true if such a division is possible, false otherwise.

Key constraints: Each number must be used exactly once, and each group must contain exactly k consecutive integers.

Input & Output

example_1.py โ€” Basic consecutive groups
$ Input: nums = [1,2,3,3,4,4,5,6], k = 4
โ€บ Output: true
๐Ÿ’ก Note: We can divide the array into two groups: [1,2,3,4] and [3,4,5,6]. Each group has exactly 4 consecutive numbers.
example_2.py โ€” Impossible division
$ Input: nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3
โ€บ Output: true
๐Ÿ’ก Note: We can form groups: [1,2,3], [2,3,4], [3,4,5], and [9,10,11]. All groups have exactly 3 consecutive numbers.
example_3.py โ€” Array length not divisible by k
$ Input: nums = [1,2,3,4], k = 3
โ€บ Output: false
๐Ÿ’ก Note: Array length (4) is not divisible by k (3), so it's impossible to divide into groups of size 3.

Visualization

Tap to expand
๐Ÿƒ Card Dealer's Algorithm VisualizationInitial Cards: [1,1,2,2,3,3,6] with k=31ร—22ร—23ร—26ร—1Step 1: Start from smallest (1), form sequence [1,2,3]Sequence: 1โ†’2โ†’3Consume: 1ร—2, 2ร—2, 3ร—2Remaining: 1ร—0, 2ร—0, 3ร—0, 6ร—1Step 2: Next smallest is 6, need [6,7,8] but 7,8 missing!6ร—17missing8missingโŒ IMPOSSIBLE๐ŸŽฏ Result: Cannot form groups of 3 consecutive numbers
Understanding the Visualization
1
Count Your Cards
First, count how many of each card value you have
2
Start from Smallest
Always begin forming sequences from the smallest available card
3
Form Complete Sequences
Create groups of exactly k consecutive cards
4
Remove Used Cards
Remove cards from your pile as they're used in sequences
5
Repeat Until Done
Continue until all cards are used or impossible to proceed
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach of always starting from the smallest available number is optimal because it ensures we never waste opportunities to form valid consecutive sequences.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

O(log n) per insertion/deletion in TreeMap, O(n) total operations

n
2n
โšก Linearithmic
Space Complexity
O(n)

TreeMap stores at most n unique numbers with their counts

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • 1 โ‰ค k โ‰ค nums.length
  • Each number in nums can be used exactly once
  • Groups must contain exactly k consecutive integers
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
78.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen