2Brute2Best
πŸ”₯0

Partition Array According to Given Pivot

MediumΒ·LeetCode #2161 Pro OnlyTricky / Edge Case
LeetCode
01

Problem Statement

You are given an integer array nums and a number pivot (which appears in nums at least once).

Rearrange the numbers so that:

  • every number smaller than pivot comes first,
  • then every number equal to pivot,
  • then every number bigger than pivot.

The catch: inside the smaller group, the numbers must stay in the same order as in nums. The same goes for the bigger group. Return the rearranged array.

Input

An integer array nums and an integer pivot that is one of the values in nums.

Output

A new array: the smaller numbers in their original order, then all the pivots, then the bigger numbers in their original order.

Example 1
Input: nums = [9, 12, 5, 10, 14, 3, 10], pivot = 10
Output: [9, 5, 3, 10, 10, 12, 14]

Smaller than 10: 9, 5, 3 (in that order in nums). Equal: 10, 10. Bigger: 12, 14 (12 comes before 14 in nums). [9, 5, 3, 10, 10, 14, 12] would be wrong, because 14 jumped ahead of 12.

Example 2
Input: nums = [-3, 4, 3, 2], pivot = 2
Output: [-3, 2, 4, 3]

Smaller: -3. Equal: 2. Bigger: 4, 3 β€” kept in their original order, even though 4 > 3.

Constraints
  • β€’ 1 ≀ nums.length ≀ 10^5
  • β€’ βˆ’10^6 ≀ nums[i] ≀ 10^6
  • β€’ pivot equals at least one element of nums
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œsmaller, then equal, then bigger than pivot”
β†’ Three fixed groups in a fixed order β†’ partition (like Sort Colors).
β€œrelative order must be kept”
β†’ The partition must be **stable**. Swapping across the array is not allowed β†’ copy in reading order instead.
β€œreturn the rearranged array”
β†’ We may build a new array, so we can write each group straight into its final place from either end.
03

Approach Complexity Ladder

Brute Force β€” stable sort by groupO(N log N)O(N)

Give each number a group (0/1/2) and stable-sort by it

Better β€” three lists, then joinO(N)O(N)

One pass drops each number into its group's list; glue the lists

Optimal β€” fill the answer from both endsO(N)O(1)

Front reader writes smaller numbers at lo++, back reader writes bigger numbers at hi--

Brute Force Intuition

Give each number a group: 0 if it is smaller than pivot, 1 if equal, 2 if bigger. Sort by the group with stable_sort, which never changes the order of numbers that compare equal. So each group keeps its original order.

Recognition rule for this sub-pattern:

A few fixed categories (0/1/2, even/odd, <pivot/=pivot/>pivot). Keep each finished region behind its own pointer.
Remember this Β· Two Pointers
Trigger
Sorted array, pairs/triplets with a target, comparing from both ends, compacting an array in place.
Think
If I compare the elements at two positions, can I safely discard one side?
Key question
Which pointer moves, and why can the skipped elements never be the answer?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Two Pointers translate into production architectures and low-level memory mechanics.