LeetCode 2161: Partition Array According to Given Pivot – Java Easy Stable Partition Solution

Learn how to solve LeetCode 2161 Partition Array According to Given Pivot using Java. Includes intuition, stable partition logic, dry run, optimized solution, and time complexity analysis.

Krishna Shrivastava
3 views
LinkedInGithubX
0
0
LeetCode 2161: Partition Array According to Given Pivot – Java Easy Stable Partition Solution
Listen to articleAudio version
Ad
Advertisement

Introduction

LeetCode 2161 is a clean and important array manipulation problem that tests:

  1. Array traversal
  2. Stable partitioning
  3. Order preservation
  4. Two-pass and three-pass approaches
  5. 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:

  1. The intuition behind stable partitioning
  2. Why order preservation matters
  3. Step-by-step explanation
  4. Dry run
  5. Time and space complexity
  6. Complete optimized Java solution

Problem Statement

Try this probelm here :- Partition Array

You are given:

  1. An integer array:
nums
  1. An integer:
pivot

You must rearrange the array such that:

  1. All elements smaller than pivot come first
  2. All elements equal to pivot come next
  3. All elements greater than pivot come last
  4. Relative ordering must remain preserved

Return the rearranged array.

Example

Input

nums = [9,12,5,10,14,3,10]
pivot = 10

Output

[9,5,3,10,10,12,14]

Explanation

Elements less than 10

9, 5, 3

Elements equal to 10

10, 10

Elements greater than 10

12, 14

All relative orderings are preserved.

Key Observation

We do NOT need sorting.

We only need grouping while maintaining original order.

This is called:

Stable Partitioning

Approach

We create a new array and fill it in 3 phases:

Phase 1

Insert all elements:

< pivot

Phase 2

Insert all elements:

== pivot

Phase 3

Insert all elements:

> pivot

This naturally preserves relative ordering because we traverse left-to-right every time.

Optimized Java Solution

class Solution {

public int[] pivotArray(int[] nums, int pivot) {

int[] ans = new int[nums.length];

int index = 0;

// Smaller than pivot
for(int i = 0; i < nums.length; i++) {

if(nums[i] < pivot) {

ans[index] = nums[i];
index++;
}
}

// Equal to pivot
for(int i = 0; i < nums.length; i++) {

if(nums[i] == pivot) {

ans[index] = nums[i];
index++;
}
}

// Greater than pivot
for(int i = 0; i < nums.length; i++) {

if(nums[i] > pivot) {

ans[index] = nums[i];
index++;
}
}

return ans;
}
}

Step-by-Step Explanation

Step 1: Create Answer Array

int[] ans = new int[nums.length];

Stores final partitioned result.

Step 2: Insert Smaller Elements

if(nums[i] < pivot)

All smaller elements go first.

Step 3: Insert Equal Elements

if(nums[i] == pivot)

Pivot values are placed in the middle.

Step 4: Insert Larger Elements

if(nums[i] > pivot)

Larger elements go to the end.

Dry Run

Input

nums = [9,12,5,10,14,3,10]
pivot = 10

Pass 1: Smaller Elements

Add:

9, 5, 3

Current array:

[9,5,3]

Pass 2: Equal Elements

Add:

10, 10

Current array:

[9,5,3,10,10]

Pass 3: Greater Elements

Add:

12, 14

Final array:

[9,5,3,10,10,12,14]

Time Complexity

We traverse the array 3 times.

Time Complexity

O(N)

Because:

3N → O(N)

Space Complexity

We use an extra array.

Space Complexity

O(N)

Why This Problem is Important

This problem teaches:

  1. Stable partitioning
  2. Array grouping
  3. Relative order preservation
  4. Multi-pass array processing
  5. Clean implementation strategy

These concepts are frequently used in:

  1. Sorting systems
  2. Data pipelines
  3. Stream processing
  4. 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:

< pivot

and

> pivot

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:

  1. Understanding of stable operations
  2. Strong array fundamentals
  3. Clean coding approach

Alternative Approach

Another approach is using:

  1. Three separate lists
  2. Then merging them

Example:

small + equal + large

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.

Ai Assistant Kas