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.
pi
, pj
where pi
is the new position of the ith
element and pj
is the new position of the jth
element. If i < j
and both elements are smaller (or larger) than pivot
, then pi < pj
.Return nums
after the rearrangement.
Example 1:
Input: 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.
Example 2:
Input: nums = [-3,4,3,2], pivot = 2 Output: [-3,2,4,3] Explanation: The element -3 is less than the pivot so it is on the left side of the array. The elements 4 and 3 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. [-3] and [4, 3] are the respective orderings.
Constraints:
1 <= nums.length <= 105
-106 <= nums[i] <= 106
pivot
equals to an element of nums
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to partitioning an array around a pivot involves checking every possible arrangement of the numbers. We will separate the numbers into three groups based on their comparison to the pivot value. We'll try every single possible ordering to find the one that satisfies the partition requirement.
Here's how the algorithm would work step-by-step:
def partition_array_brute_force(numbers, pivot):
all_arrangements = []
def generate_arrangements(index, less_than, equal_to, greater_than):
if index == len(numbers):
# We've placed all numbers, check order
all_arrangements.append((less_than, equal_to, greater_than))
return
current_number = numbers[index]
# Try putting the number in 'less than'
generate_arrangements(index + 1, less_than + [current_number], equal_to, greater_than)
# Try putting the number in 'equal to'
generate_arrangements(index + 1, less_than, equal_to + [current_number], greater_than)
# Try putting the number in 'greater than'
generate_arrangements(index + 1, less_than, equal_to, greater_than + [current_number])
generate_arrangements(0, [], [], [])
for less_than_numbers, equal_to_numbers, greater_than_numbers in all_arrangements:
#Check if the current arrangement is correctly partitioned
is_correctly_partitioned = True
reconstructed_array = less_than_numbers + equal_to_numbers + greater_than_numbers
less_than_group = [num for num in reconstructed_array if num < pivot]
equal_to_group = [num for num in reconstructed_array if num == pivot]
greater_than_group = [num for num in reconstructed_array if num > pivot]
reconstructed_array_check = less_than_group + equal_to_group + greater_than_group
if reconstructed_array != reconstructed_array_check:
is_correctly_partitioned = False
if is_correctly_partitioned:
#Ensures original order is considered when reconstructing the array
result = less_than_numbers + equal_to_numbers + greater_than_numbers
return result
return []
The best way to rearrange the numbers around a specific value is to create three separate groups: numbers smaller than the value, numbers equal to the value, and numbers bigger than the value. Then, we simply put these groups back together in the correct order to get the final result.
Here's how the algorithm would work step-by-step:
def partition_array(numbers, pivot_value):
less_than_pivot = []
equal_to_pivot = []
greater_than_pivot = []
# Segregate numbers into three lists
for number in numbers:
if number < pivot_value:
less_than_pivot.append(number)
elif number == pivot_value:
equal_to_pivot.append(number)
else:
greater_than_pivot.append(number)
# Combining the three lists into a single result
return less_than_pivot + equal_to_pivot + greater_than_pivot
def partition_array_inplace(numbers, pivot_value):
less_than = []
equal_to = []
greater_than = []
# Create 3 lists based on element's comparison to pivot.
for number in numbers:
if number < pivot_value:
less_than.append(number)
elif number == pivot_value:
equal_to.append(number)
else:
greater_than.append(number)
# Reconstruct the original array in place.
numbers[:] = less_than + equal_to + greater_than
def partition_array_optimal(numbers, pivot_value):
smaller_numbers = []
equal_numbers = []
larger_numbers = []
# Separate numbers to smaller, equal, and larger groups.
for number in numbers:
if number < pivot_value:
smaller_numbers.append(number)
elif number == pivot_value:
equal_numbers.append(number)
else:
larger_numbers.append(number)
# First create smaller list,
partitioned_array = smaller_numbers
# Then the equal list,
partitioned_array.extend(equal_numbers)
# Finally add the larger list.
partitioned_array.extend(larger_numbers)
return partitioned_array
Case | How to Handle |
---|---|
Null or empty input array | Return an empty array or throw an exception, depending on problem specifications. |
Array with a single element | Return the array as is, since it is already partitioned. |
Pivot is smaller than all elements in the array | The array will be partitioned into elements smaller than pivot followed by elements equal/greater than pivot. |
Pivot is larger than all elements in the array | The array will be partitioned into elements smaller than pivot followed by elements equal/greater than pivot. |
Array contains many duplicate elements equal to the pivot | Ensure the algorithm places all pivot values in the 'equal' partition correctly. |
Array contains negative numbers, zeros, and positive numbers | The partitioning logic should handle all number types correctly based on their comparison with the pivot. |
Large array size exceeding available memory | Consider an in-place partitioning algorithm to minimize memory usage if applicable. |
Pivot is a floating-point number and the array contains integers. | Ensure proper type conversion and comparison between the pivot and array elements. |