Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find partition array into disjoint intervals in Python
Given an array nums, we need to partition it into two disjoint subarrays called left and right such that:
Each element in left subarray is less than or equal to each element in right subarray
Both left and right subarrays are non-empty
Left subarray has the smallest possible size
We have to find the length of left after such a partitioning.
So, if the input is like nums = [5,0,3,8,6], then the output will be 3 because left array will be [5,0,3] and right subarray will be [8,6].
Approach
The key insight is to track the maximum element seen so far and find the earliest position where we can make a valid partition. We need to ensure that the maximum element in the left part is less than or equal to the minimum element in the right part.
To solve this, we will follow these steps:
Initialize variables to track maximum elements and partition position
Iterate through the array and update the partition boundary when needed
Keep track of the rightmost position where a valid partition can be made
Return the length of the left partition
Example
Let us see the following implementation to get better understanding:
def solve(nums):
max_left = None
partition_pos = None
next_max = None
current_pos = 0
for element in nums:
if max_left is None:
max_left = element
next_max = element
partition_pos = current_pos
current_pos += 1
continue
if element >= max_left:
current_pos += 1
if element > next_max:
next_max = element
continue
else:
partition_pos = current_pos
current_pos += 1
max_left = next_max
continue
return partition_pos + 1
# Test with example
nums = [5, 0, 3, 8, 6]
result = solve(nums)
print("Length of left partition:", result)
print("Left partition:", nums[:result])
print("Right partition:", nums[result:])
Length of left partition: 3 Left partition: [5, 0, 3] Right partition: [8, 6]
How It Works
The algorithm works by maintaining:
max_left: Maximum element that can be in the left partition
next_max: Maximum element seen so far in current iteration
partition_pos: Current valid partition position
When we encounter an element smaller than max_left, we need to extend the left partition to include this element, ensuring all smaller elements are in the left part.
Step-by-Step Example
For array [5, 0, 3, 8, 6]:
def solve_with_steps(nums):
max_left = None
partition_pos = None
next_max = None
current_pos = 0
print(f"Processing array: {nums}")
print("-" * 40)
for i, element in enumerate(nums):
print(f"Step {i+1}: Processing element {element}")
if max_left is None:
max_left = element
next_max = element
partition_pos = current_pos
print(f" Initial: max_left={max_left}, partition_pos={partition_pos}")
elif element >= max_left:
if element > next_max:
next_max = element
print(f" Updated next_max to {next_max}")
print(f" Element >= max_left, continue")
else:
partition_pos = current_pos
max_left = next_max
print(f" Element < max_left, update partition_pos={partition_pos}, max_left={max_left}")
current_pos += 1
print(f" Current state: partition_pos={partition_pos}, max_left={max_left}")
print()
return partition_pos + 1
nums = [5, 0, 3, 8, 6]
result = solve_with_steps(nums)
print(f"Final result: {result}")
Processing array: [5, 0, 3, 8, 6] ---------------------------------------- Step 1: Processing element 5 Initial: max_left=5, partition_pos=0 Current state: partition_pos=0, max_left=5 Step 2: Processing element 0 Element < max_left, update partition_pos=1, max_left=5 Current state: partition_pos=1, max_left=5 Step 3: Processing element 3 Element < max_left, update partition_pos=2, max_left=5 Current state: partition_pos=2, max_left=5 Step 4: Processing element 8 Updated next_max to 8 Element >= max_left, continue Current state: partition_pos=2, max_left=5 Step 5: Processing element 6 Element >= max_left, continue Current state: partition_pos=2, max_left=5 Final result: 3
Conclusion
This algorithm efficiently finds the minimum length left partition by tracking the maximum elements and updating the partition boundary when smaller elements are encountered. The time complexity is O(n) and space complexity is O(1).
