Divide Array Into Increasing Sequences - Problem

Given a sorted integer array nums in non-decreasing order and an integer k, your task is to determine if this array can be divided into disjoint increasing subsequences where each subsequence has at least k elements.

An increasing subsequence means each element must be strictly greater than the previous element in the same subsequence. The subsequences must be disjoint, meaning each element from the original array can only be used in exactly one subsequence.

Example: For array [1,2,2,3,3,4,4] and k=3, we can form two increasing subsequences: [1,2,3,4] and [2,3,4], so return true.

Input & Output

example_1.py โ€” Basic Valid Case
$ Input: nums = [1,2,2,3,3,4,4], k = 3
โ€บ Output: true
๐Ÿ’ก Note: We can form two valid increasing subsequences: [1,2,3,4] and [2,3,4]. Both have at least 3 elements and are strictly increasing.
example_2.py โ€” Insufficient Elements
$ Input: nums = [5,6,6,7,8], k = 3
โ€บ Output: false
๐Ÿ’ก Note: We cannot form valid subsequences. We have two 6's but need them in separate increasing subsequences, which would require at least two 7's and two 8's, but we only have one of each.
example_3.py โ€” Edge Case k=1
$ Input: nums = [1,2,3,4], k = 1
โ€บ Output: true
๐Ÿ’ก Note: When k=1, we can always divide the array since each element can form its own subsequence of length 1.

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค k โ‰ค nums.length
  • -105 โ‰ค nums[i] โ‰ค 105
  • nums is sorted in non-decreasing order

Visualization

Tap to expand
๐Ÿ† Tournament Team FormationPlayers by Skill Level:12233Team Formation (k=3):Team 1:[Skill 1] โ†’ [Skill 2] โ†’ [Skill 3]Team 2:[Skill 2] โ†’ [Skill 3]Greedy Assignment Process:Step 1: Skill 1Available: 1Start 1 new teamNeed: 1 skill-2, 1 skill-3Step 2: Skill 2Available: 2Use 1 for existing teamStart 1 new teamStep 3: Skill 3Available: 2Need: 2 (perfect!)All teams completeโœ… Success: All teams have โ‰ฅ3 members with increasing skills!
Understanding the Visualization
1
Count Players by Skill
First, count how many players we have at each skill level
2
Process Skills in Order
Handle players from lowest to highest skill level
3
Assign to Existing Teams
First, assign players to teams that are already waiting for this skill level
4
Form New Teams
Use remaining players to start new teams, each needing k-1 more players
Key Takeaway
๐ŸŽฏ Key Insight: We can solve this optimally by counting frequencies and using a greedy approach - always satisfy existing subsequences first, then start new ones with remaining elements.
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
28.5K Views
Medium Frequency
~25 min Avg. Time
892 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