Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the following conditions are satisfied:
pivot appears before every element greater than pivot.pivot appears in between the elements less than and greater than pivot.pivot and the elements greater than pivot is maintained.
pi, pj where pi is the new position of the ith element and pj is the new position of the jth element. If i < j and both elements are smaller (or larger) than pivot, then pi < pj.Return nums after the rearrangement.
Example 1:
Input: nums = [9,12,5,10,14,3,10], pivot = 10 Output: [9,5,3,10,10,12,14] Explanation: The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array. The elements 12 and 14 are greater than the pivot so they are on the right side of the array. The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings.
Example 2:
Input: nums = [-3,4,3,2], pivot = 2 Output: [-3,2,4,3] Explanation: The element -3 is less than the pivot so it is on the left side of the array. The elements 4 and 3 are greater than the pivot so they are on the right side of the array. The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.
Constraints:
1 <= nums.length <= 105-106 <= nums[i] <= 106pivot equals to an element of nums.Problem summary: You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the following conditions are satisfied: Every element less than pivot appears before every element greater than pivot. Every element equal to pivot appears in between the elements less than and greater than pivot. The relative order of the elements less than pivot and the elements greater than pivot is maintained. More formally, consider every pi, pj where pi is the new position of the ith element and pj is the new position of the jth element. If i < j and both elements are smaller (or larger) than pivot, then pi < pj. Return nums after the rearrangement.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
[9,12,5,10,14,3,10] 10
[-3,4,3,2] 2
partition-list)rearrange-array-elements-by-sign)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2161: Partition Array According to Given Pivot
class Solution {
public int[] pivotArray(int[] nums, int pivot) {
int n = nums.length;
int[] ans = new int[n];
int k = 0;
for (int x : nums) {
if (x < pivot) {
ans[k++] = x;
}
}
for (int x : nums) {
if (x == pivot) {
ans[k++] = x;
}
}
for (int x : nums) {
if (x > pivot) {
ans[k++] = x;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2161: Partition Array According to Given Pivot
func pivotArray(nums []int, pivot int) []int {
var ans []int
for _, x := range nums {
if x < pivot {
ans = append(ans, x)
}
}
for _, x := range nums {
if x == pivot {
ans = append(ans, x)
}
}
for _, x := range nums {
if x > pivot {
ans = append(ans, x)
}
}
return ans
}
# Accepted solution for LeetCode #2161: Partition Array According to Given Pivot
class Solution:
def pivotArray(self, nums: List[int], pivot: int) -> List[int]:
a, b, c = [], [], []
for x in nums:
if x < pivot:
a.append(x)
elif x == pivot:
b.append(x)
else:
c.append(x)
return a + b + c
// Accepted solution for LeetCode #2161: Partition Array According to Given Pivot
/**
* [2161] Partition Array According to Given Pivot
*
* You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the following conditions are satisfied:
*
* Every element less than pivot appears before every element greater than pivot.
* Every element equal to pivot appears in between the elements less than and greater than pivot.
* The relative order of the elements less than pivot and the elements greater than pivot is maintained.
*
* More formally, consider every pi, pj where pi is the new position of the i^th element and pj is the new position of the j^th element. If i < j and both elements are smaller (or larger) than pivot, then pi < pj.
*
*
*
* Return nums after the rearrangement.
*
* Example 1:
*
* Input: nums = [9,12,5,10,14,3,10], pivot = 10
* Output: [9,5,3,10,10,12,14]
* Explanation:
* The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
* The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
* The relative ordering of the elements less than and greater than pivot is also maintained. [9, 5, 3] and [12, 14] are the respective orderings.
*
* Example 2:
*
* Input: nums = [-3,4,3,2], pivot = 2
* Output: [-3,2,4,3]
* Explanation:
* The element -3 is less than the pivot so it is on the left side of the array.
* The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
* The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.
*
*
* Constraints:
*
* 1 <= nums.length <= 10^5
* -10^6 <= nums[i] <= 10^6
* pivot equals to an element of nums.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/partition-array-according-to-given-pivot/
// discuss: https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn pivot_array(nums: Vec<i32>, pivot: i32) -> Vec<i32> {
let (mut less, mut equal, mut greater) = (Vec::new(), Vec::new(), Vec::new());
nums.into_iter().for_each(|num| match num.cmp(&pivot) {
std::cmp::Ordering::Less => less.push(num),
std::cmp::Ordering::Equal => equal.push(num),
std::cmp::Ordering::Greater => greater.push(num),
});
[less, equal, greater].concat()
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2161_example_1() {
let nums = vec![9, 12, 5, 10, 14, 3, 10];
let pivot = 10;
let result = vec![9, 5, 3, 10, 10, 12, 14];
assert_eq!(Solution::pivot_array(nums, pivot), result);
}
#[test]
fn test_2161_example_2() {
let nums = vec![-3, 4, 3, 2];
let pivot = 2;
let result = vec![-3, 2, 4, 3];
assert_eq!(Solution::pivot_array(nums, pivot), result);
}
}
// Accepted solution for LeetCode #2161: Partition Array According to Given Pivot
function pivotArray(nums: number[], pivot: number): number[] {
const ans: number[] = [];
for (const x of nums) {
if (x < pivot) {
ans.push(x);
}
}
for (const x of nums) {
if (x === pivot) {
ans.push(x);
}
}
for (const x of nums) {
if (x > pivot) {
ans.push(x);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.