Introduction
LeetCode 2161 is a clean and important array manipulation problem that tests:
- Array traversal
- Stable partitioning
- Order preservation
- Two-pass and three-pass approaches
- Logical grouping of elements
The interesting part of this problem is that we are not only partitioning the array around a pivot, but we also need to preserve the relative order of elements.
This makes the problem slightly different from classical partition algorithms like QuickSort partitioning.
In this article, we will understand:
- The intuition behind stable partitioning
- Why order preservation matters
- Step-by-step explanation
- Dry run
- Time and space complexity
- Complete optimized Java solution
Problem Statement
Try this probelm here :- Partition Array
You are given:
- An integer array:
- An integer:
You must rearrange the array such that:
- All elements smaller than pivot come first
- All elements equal to pivot come next
- All elements greater than pivot come last
- Relative ordering must remain preserved
Return the rearranged array.
Example
Input
Output
Explanation
Elements less than 10
Elements equal to 10
Elements greater than 10
All relative orderings are preserved.
Key Observation
We do NOT need sorting.
We only need grouping while maintaining original order.
This is called:
Approach
We create a new array and fill it in 3 phases:
Phase 1
Insert all elements:
Phase 2
Insert all elements:
Phase 3
Insert all elements:
This naturally preserves relative ordering because we traverse left-to-right every time.
Optimized Java Solution
Step-by-Step Explanation
Step 1: Create Answer Array
Stores final partitioned result.
Step 2: Insert Smaller Elements
All smaller elements go first.
Step 3: Insert Equal Elements
Pivot values are placed in the middle.
Step 4: Insert Larger Elements
Larger elements go to the end.
Dry Run
Input
Pass 1: Smaller Elements
Add:
Current array:
Pass 2: Equal Elements
Add:
Current array:
Pass 3: Greater Elements
Add:
Final array:
Time Complexity
We traverse the array 3 times.
Time Complexity
Because:
Space Complexity
We use an extra array.
Space Complexity
Why This Problem is Important
This problem teaches:
- Stable partitioning
- Array grouping
- Relative order preservation
- Multi-pass array processing
- Clean implementation strategy
These concepts are frequently used in:
- Sorting systems
- Data pipelines
- Stream processing
- Partition-based algorithms
Common Mistakes
1. Using QuickSort Partition Logic
QuickSort partitioning does NOT preserve order.
This problem specifically requires stable ordering.
2. Forgetting Equal Elements
Some solutions only handle:
and
But pivot values must remain in the center.
3. Overcomplicating with Two Pointers
A simple three-pass solution is cleaner and easier to understand.
Interview Explanation
In interviews explain:
Since relative order must remain preserved, we cannot use traditional in-place partitioning. Instead, we perform stable partitioning by collecting smaller, equal, and larger elements sequentially.
This demonstrates:
- Understanding of stable operations
- Strong array fundamentals
- Clean coding approach
Alternative Approach
Another approach is using:
- Three separate lists
- Then merging them
Example:
But using one answer array is more space efficient.
Conclusion
LeetCode 2161 is a simple yet important stable partition problem.
The key insight is:
We must preserve relative ordering while grouping elements around the pivot.
Using a clean three-pass traversal gives an elegant and efficient O(N) solution.





