FastPrepStable Array Partition

Stable Array Partition

Roblox logoRoblox● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Partition an integer array nums around a supplied value pivot.

For this exercise, assume all values strictly less than pivot must appear first, followed by all values greater than or equal to it. Preserve the original relative order within each of those two groups.

Return the partitioned values in a new array and leave the input unchanged. Preserve duplicates, and place values equal to the pivot in the second group. An empty input returns an empty array.

Function

stablePartition(nums: int[], pivot: int) → int[]

Examples

Example 1

nums = [9,1,5,2,5,7]pivot = 5return = [1,2,9,5,5,7]

Values 1 and 2 come first. Both groups retain their input order.

Example 2

nums = [3,2,1,2]pivot = 2return = [1,3,2,2]

The value 1 is the only value strictly below 2; equal values belong to the second group.

Example 3

nums = []pivot = 0return = []

An empty input has no values to partition.

Constraints

  • 0 <= nums.length <= 1000.
  • -10^9 <= nums[i], pivot <= 10^9.

More Roblox problems

See Roblox hiring insights
public int[] stablePartition(int[] nums, int pivot) {
    // write your code here
}
nums[9,1,5,2,5,7]
pivot5
expected[1,2,9,5,5,7]
Checking account…