Check if There is a Valid Partition For The Array - Problem

Imagine you're a puzzle master trying to break down a sequence of numbers into perfect groups! ๐Ÿงฉ

You're given a 0-indexed integer array nums, and your mission is to partition it into one or more contiguous subarrays where each subarray follows one of these magical rules:

  • Twin Rule: Exactly 2 identical elements (like [5,5])
  • Triplet Rule: Exactly 3 identical elements (like [7,7,7])
  • Staircase Rule: Exactly 3 consecutive increasing elements with difference of 1 (like [3,4,5])

Your goal is to determine if you can create at least one valid partition using these rules. Return true if such a partition exists, otherwise return false.

Example: For [4,4,4,5,6], you can partition it as [4,4,4] (triplet rule) + [5,6] - wait, [5,6] doesn't follow any rule! So this would be false.

Input & Output

example_1.py โ€” Basic Valid Partition
$ Input: nums = [4,4,4,5,6]
โ€บ Output: false
๐Ÿ’ก Note: We can form [4,4,4] (triplet rule) but [5,6] doesn't satisfy any rule (needs exactly 2 or 3 elements, and they're not equal or consecutive with size 3).
example_2.py โ€” Multiple Valid Partitions
$ Input: nums = [1,1,1,2]
โ€บ Output: false
๐Ÿ’ก Note: We can partition as [1,1,1] + [2], but [2] is a single element which doesn't satisfy any rule. We could try [1,1] + [1,2] but [1,2] also doesn't work.
example_3.py โ€” Consecutive Sequence
$ Input: nums = [1,1,1,1,2,2]
โ€บ Output: true
๐Ÿ’ก Note: We can partition as [1,1,1] (triplet rule) + [1,2,2] - wait, that doesn't work. Actually [1,1] + [1,1] + [2,2] works! Three pairs following the twin rule.

Visualization

Tap to expand
Cookie Jar Partitioning AnalogyEach jar must follow one of three rulesJar RulesTwin: 2 identical ๐Ÿช๐ŸชTriplet: 3 identical ๐Ÿช๐Ÿช๐ŸชStair: 3 consecutive ๐Ÿง๐Ÿช๐ŸฅจExample: [๐Ÿช,๐Ÿช,๐Ÿช,๐Ÿฅจ,๐Ÿง]Jar 1: Valid! โœ“๐Ÿช๐Ÿช๐ŸชTriplet RuleJar 2: Invalid! โœ—๐Ÿฅจ๐ŸงNo Rule MatchesResult: Cannot partition validly!All cookies must be used and follow jar rules๐Ÿ’ก Key Insight: Use DP to check if remaining cookies can be validly organized
Understanding the Visualization
1
Examine cookie line
Look at cookies from left to right: ๐Ÿช๐Ÿช๐Ÿช๐Ÿฅจ๐Ÿง
2
Try jar rules
Can we fit first 3 cookies in one jar? Yes! (triplet rule)
3
Check remaining
Now we have ๐Ÿฅจ๐Ÿง left - can these follow any jar rule?
4
Determine validity
๐Ÿฅจ๐Ÿง are only 2 items and different - doesn't work!
Key Takeaway
๐ŸŽฏ Key Insight: At each position, we only need to know if the remaining elements can be validly partitioned - this creates a perfect dynamic programming opportunity!

Time & Space Complexity

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

Single pass through the array, constant work per position

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

DP array of size n+1 to store results for each position

n
2n
โšก Linearithmic Space

Constraints

  • 2 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 106
  • Array must be partitioned completely (no elements left out)
  • All subarrays must be contiguous
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
23.5K Views
Medium-High Frequency
~18 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