Divide Array Into Increasing Sequences - Problem

Given an integer array nums sorted in non-decreasing order and an integer k, return true if this array can be divided into one or more disjoint increasing subsequences of length at least k, or false otherwise.

A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. Two subsequences are disjoint if they do not share any elements.

An increasing subsequence means each element is strictly greater than the previous one.

Input & Output

Example 1 — Valid Partition
$ Input: nums = [1,2,3,3,4,5], k = 3
Output: true
💡 Note: We can form one increasing subsequence [1,2,3,4,5] of length 5 ≥ 3 by using elements at positions [0,1,2,4,5], leaving the duplicate 3 unused.
Example 2 — Too Small Array
$ Input: nums = [1,2], k = 3
Output: false
💡 Note: Array length is 2, but we need subsequences of length at least 3, so it's impossible.
Example 3 — Frequency Too High
$ Input: nums = [1,1,1,2], k = 2
Output: false
💡 Note: Number 1 appears 3 times, but with k=2, we can have at most ⌈4/2⌉ = 2 occurrences of any number.

Constraints

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

Visualization

Tap to expand
Divide Array Into Increasing Sequences INPUT nums array (sorted): 1 2 3 3 4 5 0 1 2 3 4 5 Duplicate: 3 appears twice nums = [1,2,3,3,4,5] k = 3 (min length) Can we split into disjoint increasing subsequences each of length >= k? n = 6 elements Max freq = 2 (value 3) ALGORITHM (Greedy) 1 Find Max Frequency Count most repeated element maxFreq = 2 (value 3 appears 2x) 2 Calculate Groups Needed groups = maxFreq = 2 3 Check Feasibility n / groups >= k ? 6 / 2 = 3 3 >= 3 ? YES! 4 Return Result n >= k * maxFreq 6 >= 3 * 2 --> true FINAL RESULT Subsequence 1: 1 3 4 len=3 OK Subsequence 2: 2 3 5 len=3 OK Verification: [1,3,4] - strictly increasing [2,3,5] - strictly increasing Both disjoint, both len >= 3 Output: true Division possible! OK - Valid Split Key Insight: The maximum frequency of any element determines the minimum number of groups needed. If we have g groups, each must have at least k elements. So we need: n >= k * maxFreq This greedy approach works because sorted arrays allow optimal distribution of elements. TutorialsPoint - Divide Array Into Increasing Sequences | Greedy Approach
Asked in
Google 15 Amazon 12 Microsoft 8
15.6K Views
Medium Frequency
~25 min Avg. Time
423 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