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 left is less than or equal to every element in right.
  • left and right are non-empty.
  • left has 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
Partition Array into Disjoint IntervalsFind minimal left partition where max(left) ≤ min(right)50386LEFTRIGHTPARTITIONmax([5,0,3]) = 5min([8,6]) = 65 ≤ 6 ✓ Valid partition!Length of left partition: 3
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)
Asked in
Google 42 Facebook 38 Microsoft 31 Amazon 29
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