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
pivot
appears before every element greater thanpivot
.Every element equal to
pivot
appears in between the elements less than and greater thanpivot
.The relative order of the elements less than
pivot
and the elements greater thanpivot
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 thej^th
element. Ifi < j
and 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^6
pivot
equals to an element ofnums
.
Hints/Notes
- 2025/03/09 Q3
- two pointers
- 0x3Fâs solution
Solution
Language: C++
1 | class Solution { |