Partition Array into Disjoint Intervals - Problem
Given an integer array nums, partition it into two contiguous subarrays left and right so that:
- Every element in
leftis less than or equal to every element inright. leftandrightare non-empty.lefthas the smallest possible size.
Return the length of left after such a partitioning.
Test cases are generated such that partitioning exists.
Input & Output
Example 1 — Basic Partition
$
Input:
nums = [5,0,3,8,6]
›
Output:
3
💡 Note:
Partition into left=[5,0,3] and right=[8,6]. Max of left is 5, min of right is 6. Since 5 ≤ 6, this is valid. Length of left is 3.
Example 2 — All Left Except Last
$
Input:
nums = [1,1,1,2]
›
Output:
3
💡 Note:
Partition into left=[1,1,1] and right=[2]. Max of left is 1, min of right is 2. Since 1 ≤ 2, this is valid. Length of left is 3.
Example 3 — Minimum Valid Partition
$
Input:
nums = [1,2]
›
Output:
1
💡 Note:
Partition into left=[1] and right=[2]. Max of left is 1, min of right is 2. Since 1 ≤ 2, this is valid. Length of left is 1.
Constraints
- 2 ≤ nums.length ≤ 105
- 0 ≤ nums[i] ≤ 106
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Array [5,0,3,8,6] needs valid partition
2
Find Partition
Check where max(left) ≤ min(right)
3
Output Length
Return length of left partition: 3
Key Takeaway
🎯 Key Insight: The optimal partition minimizes left size while ensuring max(left) ≤ min(right)
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code