Problem
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
wherepi
is the new position of theith
element andpj
is the new position of thejth
element. For elements less thanpivot
, ifi < j
andnums[i] < pivot
andnums[j] < pivot
, thenpi < pj
. Similarly for elements greater thanpivot
, ifi < j
andnums[i] > pivot
andnums[j] > pivot
, thenpi < pj
.
- More formally, consider every
Return nums
after the rearrangement.
Examples
Example 1:
|
|
Example 2:
|
|
Constraints:
1 <= nums.length <= 105
-106 <= nums[i] <= 106
pivot
equals to an element ofnums
.
Solution
Method 1 - Creating 3 lists and update the arrays
To rearrange the elements of the array based on the given pivot, we need to maintain three separate lists:
- One for elements less than the pivot.
- One for elements equal to the pivot.
- One for elements greater than the pivot.
By iterating through the array and appending each element to the appropriate list, we effectively partition the array into the required format while maintaining relative order. Finally, we concatenate these three lists to obtain the rearranged array.
Approach
- Initialize Three Lists:
- One for elements less than
pivot
. - One for elements equal to
pivot
. - One for elements greater than
pivot
.
- One for elements less than
- Iterate Through the Original List:
- Append each element to the corresponding list based on its comparison with
pivot
.
- Append each element to the corresponding list based on its comparison with
- Concatenate the Three Lists:
- Combine all three lists to get the partitioned list.
- Return the Partitioned List:
- Return the concatenated list as the result.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
, wheren
is the number of elements innums
, as we are iterating through the list once. - 🧺 Space complexity:
O(n)
, wheren
is the number of elements innums
, for storing elements in the three separate lists.
Method 2 - Using Two Pointer Approach
We can also use two pointer technique:
- Using the two-pointer approach, we process the
nums
array in one traversal while maintaining relative order.lessIdx
to place elements less thanpivot
at the beginning of the array.greaterIdx
to place elements greater thanpivot
from the end of the array.
- Create an auxiliary array
ans
of the same length asnums
to store the rearranged numbers.
Here is the algorithm:
- Numbers
< pivot
get placed sequentially vialessIdx
. - Numbers
> pivot
get stored viagreaterIdx
- All occurrences of
pivot
fill the middle (while
loop) once the pointers converge.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
as we traverse the array three times with linear time operations. - 🧺 Space complexity:
O(n)
due to the use of the auxiliary arrayans
.