Introduction
The Partition Array problem involves rearranging elements based on a condition.
Given:
- an array
- a pivot condition
the task is to:
- place elements satisfying condition on one side
- place remaining elements on other side
This problem is one of the most important applications of:
Partitioning TechniqueThis concept is heavily used in:
- Quick Sort
- Dutch National Flag Algorithm
- Two Pointer problems
This problem helps in understanding:
- partitioning
- swapping
- two pointers
- in-place rearrangement
Example
Input:arr = [9, 12, 3, 5, 14, 10, 10]
pivot = 10
Output:
[9, 3, 5, 10, 14, 10, 12]
Explanation:
Elements smaller than pivot are moved left side.
Constraints
1 <= arr.length <= 10^5-10^9 <= arr[i] <= 10^9Approach 1 : Extra Array Method
Explanation
The simplest way to solve this problem is:
- Traverse array
- Store smaller elements
- Store greater elements
- Combine results
This approach is easy but uses extra space.
Steps
- Create result array.
- Store smaller elements.
- Store equal elements.
- Store larger elements.
- Return partitioned array.
Dry Run
Input:[9, 12, 3, 5, 14, 10, 10]
pivot = 10
Smaller elements:
[9, 3, 5]
Equal elements:
[10, 10]
Larger elements:
[12, 14]
Combined result:
[9, 3, 5, 10, 10, 12, 14]
Final Result:
[9, 3, 5, 10, 10, 12, 14]
Extra Array Method Code
Complexity Analysis
Time Complexity: O(n)
Explanation:
Array is traversed once.
Space Complexity: O(n)
Explanation:
Extra arrays are used.Approach 2 : Two Pointer Partitioning
Explanation
The optimized solution uses:
Two Pointers
The idea is:
- Maintain partition index
- Move smaller elements left
- Swap elements in-place
This is the same logic used in:
- Quick Sort partitioning
The optimized solution uses:
Two PointersThe idea is:
- Maintain partition index
- Move smaller elements left
- Swap elements in-place
This is the same logic used in:
- Quick Sort partitioning
Steps
- Initialize partition index.
- Traverse array.
- If current element smaller:
- swap with partition index
- Increment partition index.
- Return partitioned array.
- Initialize partition index.
- Traverse array.
- If current element smaller:
- swap with partition index
- Increment partition index.
- Return partitioned array.
Dry Run
Input:[9, 12, 3, 5, 14, 10, 10]
pivot = 10
Initial:
partition = 0
Traverse 9:
9 < 10
Swap with partition
Array:
[9, 12, 3, 5, 14, 10, 10]
partition++
Traverse 3:
3 < 10
Swap with partition
Array:
[9, 3, 12, 5, 14, 10, 10]
Continue traversal...
Final Result:
[9, 3, 5, 12, 14, 10, 10]
Two Pointer Partitioning Code
Complexity Analysis
Time Complexity: O(n)Explanation:
Array is traversed once.
Space Complexity: O(1)
Explanation:
Partitioning is performed in-place.
Edge Cases
- Empty array
- All elements smaller
- All elements larger
- Duplicate pivot values
- Already partitioned array
Why This Problem is Important
Partition Array helps in understanding:
- Partitioning techniques
- Two pointer logic
- In-place swapping
- Quick Sort foundation
- Array rearrangement
It is one of the most important partitioning interview problems.
Real-World Applications
Partitioning concepts are used in:
- Quick Sort
- Data filtering
- Memory management
- Stream segregation
- Task scheduling systems
Common Mistakes
- Incorrect partition index
- Wrong swap conditions
- Forgetting partition increment
- Confusing pivot placement
Interview Tips
Interviewers often expect:
- Partitioning explanation
- In-place optimization
- Two pointer understanding
Always explain:
- role of partition index
- why swapping works
- how smaller elements move left
Related Questions
- Sort Colors
- Quick Sort
- Move Zeroes
- Sort Array By Parity
- Quick Select
Final Takeaway
The Partition Array problem is a fundamental partitioning problem that teaches in-place rearrangement and two-pointer techniques. Understanding this problem builds a strong foundation for advanced partitioning and sorting interview problems.