2161. Partition Array According to Given Pivot Medium

@problem@discussion
#Array#Two Pointers#Simulation



1/**
2 * [2161] Partition Array According to Given Pivot
3 *
4 * You are given a 0-indexed integer array nums and an integer pivot. Rearrange nums such that the following conditions are satisfied:
5 * 
6 * 	Every element less than pivot appears before every element greater than pivot.
7 * 	Every element equal to pivot appears in between the elements less than and greater than pivot.
8 * 	The relative order of the elements less than pivot and the elements greater than pivot is maintained.
9 * 	
10 * 		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. For elements less than pivot, if i < j and nums[i] < pivot and nums[j] < pivot, then pi < pj. Similarly for elements greater than pivot, if i < j and nums[i] > pivot and nums[j] > pivot, then pi < pj.
11 * 	
12 * 	
13 * 
14 * Return nums after the rearrangement.
15 *  
16 * Example 1:
17 * 
18 * Input: nums = [9,12,5,10,14,3,10], pivot = 10
19 * Output: [9,5,3,10,10,12,14]
20 * Explanation: 
21 * The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array.
22 * The elements 12 and 14 are greater than the pivot so they are on the right side of the array.
23 * 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.
24 * 
25 * Example 2:
26 * 
27 * Input: nums = [-3,4,3,2], pivot = 2
28 * Output: [-3,2,4,3]
29 * Explanation: 
30 * The element -3 is less than the pivot so it is on the left side of the array.
31 * The elements 4 and 3 are greater than the pivot so they are on the right side of the array.
32 * The relative ordering of the elements less than and greater than pivot is also maintained. [-3] and [4, 3] are the respective orderings.
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	1 <= nums.length <= 10^5
38 * 	-10^6 <= nums[i] <= 10^6
39 * 	pivot equals to an element of nums.
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/partition-array-according-to-given-pivot/
45// discuss: https://leetcode.com/problems/partition-array-according-to-given-pivot/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn pivot_array(nums: Vec<i32>, pivot: i32) -> Vec<i32> {
51        vec![]
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_2161() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.