
Problem Statement
Given a 0-indexed integer array nums
and an integer pivot
, your task is to rearrange the elements in the array so that all elements less than the pivot come before all elements that are greater than the pivot. Additionally, all elements equal to the pivot should appear between the groups of lesser and greater elements. It's essential that within each group (less than, equal to, and greater than the pivot), the original relative order of the elements is preserved. This preserved ordering means, for example, if one element that is less than the pivot originally appears before another element less than the pivot, it should also appear before it in the transformed array.
Examples
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 ofnums
.
Approach and Intuition
The goal is to rearrange the array nums
by categorizing its elements into three distinct groups based on their comparison to the pivot
: less than, equal to, and greater than the pivot. Here is how we can approach the task:
Initialize Three Lists: Start by creating three separate lists - one for elements less than the pivot, one for elements equal to the pivot, and one for those greater than the pivot.
Distribute Elements Among Lists:
- Traverse through each element of the array.
- Compare each element with the
pivot
:- Add the element to the 'less than' list if it is smaller than the pivot.
- Add the element to the 'equal to' list if it matches the pivot.
- Add the element to the 'greater than' list if it is larger than the pivot.
Concatenate Lists: Once all elements are categorized:
- Combine (concatenate) the 'less than' list, 'equal to' list, and 'greater than' list in that specific order.
- This concatenation will form the final rearranged array where order conditions specified in the problem are met.
This method ensures that all elements less than the pivot are placed at the beginning of nums
, followed by elements equal to the pivot, and finally, those greater than the pivot. Also, the relative ordering within the groups (less than and greater than) is maintained as required. The relative order condition is inherently respected by this approach due to the way elements are appended to their respective lists solely based on their original order in nums
.
Solutions
- C++
class Solution {
public:
vector<int> rearrangeArray(vector<int>& arr, int key) {
vector<int> result(arr.size());
int lowIndex = 0;
int highIndex = arr.size() - 1;
for (int low = 0, high = arr.size() - 1; low < arr.size(); low++, high--) {
if (arr[low] < key) {
result[lowIndex] = arr[low];
lowIndex++;
}
if (arr[high] > key) {
result[highIndex] = arr[high];
highIndex--;
}
}
while (lowIndex <= highIndex) {
result[lowIndex] = key;
lowIndex++;
}
return result;
}
};
The described C++ solution focuses on rearranging an array based on a given pivot, key
. The function rearrangeArray
takes in two parameters: a vector of integers arr
and the integer key
. The goal is to reorder the elements of arr
such that all elements less than key
are on the left, all elements greater than key
are on the right, and all instances of key
(if any) fill the middle positions of the resulting vector.
Here's a breakdown of the approach:
result
vector is initialized with the same size asarr
to store the rearranged elements.- Two indices,
lowIndex
andhighIndex
, are used for placing smaller elements from the start and larger elements from the end of theresult
vector, respectively. - Iterate over the array with two pointers,
low
starting from the beginning andhigh
from the end. During the iteration:- If
arr[low]
is less thankey
, place it at theresult[lowIndex]
and incrementlowIndex
. - Conversely, if
arr[high]
is greater thankey
, place it atresult[highIndex]
and decrementhighIndex
.
- If
- After processing all elements in
arr
, fill the gap betweenlowIndex
andhighIndex
inresult
with the pivot valuekey
.
This method efficiently partitions the array into three segments using a single traversal and without extra space for another array, adhering to in-place principles. Ensure to handle edge cases, such as arrays containing all elements equal to key
, very small arrays, or arrays with no elements equal to key
.
- Java
class Solution {
public int[] rearrangeArray(int[] elements, int pivotValue) {
int[] result = new int[elements.length];
int lessIndex = 0;
int greaterIndex = elements.length - 1;
for (int i = 0, j = elements.length - 1; i < elements.length; i++, j--) {
if (elements[i] < pivotValue) {
result[lessIndex] = elements[i];
lessIndex++;
}
if (elements[j] > pivotValue) {
result[greaterIndex] = elements[j];
greaterIndex--;
}
}
while (lessIndex <= greaterIndex) {
result[lessIndex] = pivotValue;
lessIndex++;
}
return result;
}
}
This Java solution tackles the problem of reorganizing an array relative to a given pivot value. The rearrangeArray
method in the Solution
class modifies the order of elements in the input array elements
based on their comparison with pivotValue
. The resulting array places all elements less than the pivot before those greater than the pivot, with occurrences of the pivot value in between.
Here is an outline of how the method works:
- Initialize
result
, an array of the same length aselements
to store the rearranged values. - Use two index pointers,
lessIndex
andgreaterIndex
.lessIndex
starts from the beginning of the array, whilegreaterIndex
starts from the end. - Iterate through the
elements
array using two variables in a for loop,i
incrementing from the start andj
decrementing from the end. This allows simultaneous placement of smaller and larger elements in their respective positions in theresult
. - If an element indexed by
i
is less than the pivot, it's placed at the currentlessIndex
position inresult
, andlessIndex
is incremented. - Similarly, if an element indexed by
j
is greater than the pivot, it's placed at the currentgreaterIndex
position inresult
, andgreaterIndex
is decremented. - After the loop, fill up any indices between
lessIndex
andgreaterIndex
with thepivotValue
.
This method ensures a traversal efficiency of O(n), where n is the number of elements in the array, and performs the separation in one pass with additional filling of pivot values in a contiguous mid-section. This arrangement guarantees the elements relative to the pivot are placed as desired without the need for additional sorting steps.
- Python
class Solution:
def rearrangeArray(self, array, key):
output = [0] * len(array)
lower_index = 0
upper_index = len(array) - 1
for forward, backward in zip(range(len(array)), range(len(array) - 1, -1, -1)):
if array[forward] < key:
output[lower_index] = array[forward]
lower_index += 1
if array[backward] > key:
output[upper_index] = array[backward]
upper_index -= 1
while lower_index <= upper_index:
output[lower_index] = key
lower_index += 1
return output
This summary explains how the provided Python solution can rearrange an array with respect to a given pivot (key
) so that all elements less than the key
are positioned before it, and all elements greater are positioned after it.
- The solution employs a class named
Solution
with a methodrearrangeArray
taking two parameters -array
which is the list of integers to rearrange, andkey
, the pivot element. - A new list
output
is initialized with zeros to hold the rearranged elements. Its size matches that ofarray
. - Using two pointers,
lower_index
initialized at the start of the list andupper_index
initialized at the end, the method iterates through the list:- Traverse from both ends of the array simultaneously using a
for
loop.forward
moves from the beginning toward the middle, whilebackward
moves from the end backward. - If the element at the
forward
index is less thankey
, it's placed at the currentlower_index
in theoutput
, andlower_index
is incremented. - Similarly, if the element at the
backward
index is greater thankey
, it's placed at the currentupper_index
in theoutput
, andupper_index
is decremented.
- Traverse from both ends of the array simultaneously using a
- After completing the initial passes, fill any remaining positions (where
lower_index
has not yet metupper_index
) with thekey
. This accounts for elements that are exactly equal to the pivot. - The method completes by returning the
output
array now partitioned according to the specifiedkey
.
This approach ensures that the array is rearranged in a single pass with respect to the given key
, efficiently partitioning the values as required.
No comments yet.