#2161
Medium Algorithms Partition array according to given pivot
Array Two Pointers Simulation
89.8% acceptance
Feb 25, 2026
1762
123
You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that:
Every element less than pivot appears before every element greater than pivot.
Every element equal to pivot appears in between.
The relative order of elements less than pivot and greater than pivot is maintained.
Return nums after the rearrangement.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn pivot_array(nums: Vec<i32>, pivot: i32) -> Vec<i32> {
let less: Vec<i32> = nums.iter().filter(|&&x| x < pivot).cloned().collect();
let equal: Vec<i32> = nums.iter().filter(|&&x| x == pivot).cloned().collect();
let greater: Vec<i32> = nums.iter().filter(|&&x| x > pivot).cloned().collect();
[less, equal, greater].concat()
}
}