Rearrange an array so that elements smaller than a pivot come first, followed by elements equal to the pivot, then elements greater than the pivot, while preserving relative order within each group.
Given an integer array nums and an integer pivot, reorder the array into three consecutive groups:
pivotpivotpivotThe relative order of elements within each group should remain the same as in the original array.
Return the rearranged array.
numspivotnums partitioned around pivot in stable order.1 <= nums.length is assumed to be within typical interview limitsExample 1
Input
nums = [9,12,5,10,14,3,10], pivot = 10
Output
[9,5,3,10,10,12,14]
Explanation
Numbers less than 10 appear first in original order: [9,5,3]. Then the values equal to 10: [10,10]. Finally, the values greater than 10: [12,14].
Example 2
Input
nums = [-3,4,2,0,4], pivot = 4
Output
[-3,2,0,4,4]
Explanation
The elements smaller than 4 are kept first, followed by all 4s, then values greater than 4. The order inside each group stays unchanged.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.