2161. Partition Array According to Given Pivot
2161. Partition Array According to Given Pivot
Description
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
pivotappears before every element greater thanpivot.Every element equal to
pivotappears in between the elements less than and greater thanpivot.The relative order of the elements less than
pivotand the elements greater thanpivotis maintained.More formally, consider every pi, pj where pi is the new position of the
i^thelement and pj is the new position of thej^thelement. Ifi < jand both elements are smaller (or larger) thanpivot, then pi < pj.
Return nums after the rearrangement.
Example 1:
1 | Input: nums = [9,12,5,10,14,3,10], pivot = 10 |
Example 2:
1 | Input: nums = [-3,4,3,2], pivot = 2 |
Constraints:
1 <= nums.length <= 10^5-10^6 <= nums[i] <= 10^6pivotequals to an element ofnums.
Hints/Notes
- 2025/03/09 Q3
- two pointers
- 0x3Fâs solution
Solution
Language: C++
1 | class Solution { |