1359. Range Band Partition

Easy1.5 points estimatedSortingArrayTwo Pointers

A conveyor sorts items by weight reading into three bands. Given nums, lo and hi, rearrange the array nums in place so that all values below lo come first, then all values from lo to hi inclusive, then all values above hi. Inside each band the values must keep the same relative order they had in the input, so the rearrangement is stable and the result is unique. The method returns nothing: the array nums itself is read as the answer after the call. An extra buffer of size n is allowed. Aim for O(n) time and O(n) extra space.

Example 1

Input:
nums = [5,1,9,3,7], lo = 3, hi = 7
Output:
[1,5,3,7,9]
Explanation:

Only 1 is below 3, only 9 is above 7, and the middle band keeps its order 5, 3, 7.

Example 2

Input:
nums = [4,4,4], lo = 4, hi = 4
Output:
[4,4,4]
Explanation:

Every value lies inside the band [4, 4], so nothing moves.

Example 3

Input:
nums = [10,-2,6,-8,0], lo = -1, hi = 5
Output:
[-2,-8,0,10,6]
Explanation:

The values -2 and -8 are below -1, 0 is inside, and 10 and 6 are above 5, each band keeping its input order.

Constraints

1 ≤ n ≤ 1000

-109 ≤ nums[i] ≤ 109

-109 ≤ lo ≤ hi ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Graded
Your answer is read from nums after your method returns.

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…