You are given a 0-indexed integer array nums
and an integer pivot
. Rearrange nums
such that the following conditions are satisfied:
pivot
appears before every element greater than pivot
.pivot
appears in between the elements less than and greater than pivot
.pivot
and the elements greater than pivot
is maintained.
p``i```,
pj``` where `p
i is the new position of the `i``th` element and `p``j
is the new position of the j``th
element. If i < j
and both elements are smaller (or larger) than pivot
, then `pi
< p``j```.Return nums
after the rearrangement.
For example:
nums = [9,12,5,10,14,3,10], pivot = 10
Output:
[9,5,3,10,10,12,14]
Explanation: The elements 9, 5, and 3 are less than the pivot so they are on the left side of the array. The elements 12 and 14 are greater than the pivot so they are on the right side of the array. 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.
How would you implement this?
Given a 0-indexed integer array nums
and an integer pivot
, rearrange nums
such that:
pivot
appears before every element greater than pivot
.pivot
appears in between the elements less than and greater than pivot
.pivot
and elements greater than pivot
is maintained.Return the rearranged array.
A straightforward approach is to iterate through the array three times:
Finally, concatenate the collected elements in the order: less, equal, greater.
def rearrange_array_naive(nums, pivot):
less = []
equal = []
greater = []
for num in nums:
if num < pivot:
less.append(num)
elif num == pivot:
equal.append(num)
else:
greater.append(num)
return less + equal + greater
A more efficient solution involves a single pass through the array using three separate lists to hold elements less than, equal to, and greater than the pivot. This maintains the relative order while rearranging the elements.
def rearrange_array(nums, pivot):
less = []
equal = []
greater = []
for num in nums:
if num < pivot:
less.append(num)
elif num == pivot:
equal.append(num)
else:
greater.append(num)
return less + equal + greater
The optimal solution iterates through the input array nums
once. For each element, a comparison is made, and the element is appended to one of three lists. Appending to a list takes O(1) time on average.
Therefore, the overall time complexity is O(n), where n is the length of the nums
array.
Three lists, less
, equal
, and greater
, are used to store elements less than, equal to, and greater than the pivot, respectively. In the worst-case scenario, all elements could be less than the pivot, equal to the pivot, or greater than the pivot. This means one of the lists could potentially store all n elements from the input array.
Therefore, the space complexity is O(n), where n is the length of the nums
array.
nums
, so we don't need to explicitly handle this edge case.These edge cases are handled correctly by the optimal solution as the lists less
, equal
, and greater
would be constructed accordingly and concatenated correctly.