FunctionRacerProblemsRace

Three-Way Split Around a Pivot

EasyTwo pointersPython, JavaScript, TypeScript, Java, C++

Rearrange nums in place so that every item smaller than pivot comes first, then every item equal to pivot, then every item larger than pivot. Within each of the three groups the items keep the relative order they had in nums. Return nothing.

pivot may or may not appear in nums, and any of the three groups may be empty, including all of them when nums is empty.

Example 1

nums [9,12,5,10,14,3,10]
pivot 10
returns [9,5,3,10,10,12,14]

Below 10 in their old order: 9, 5, 3; then the two 10s; then 12 and 14 in their old order.

Example 2

nums [-3,4,3,4,1]
pivot 2
returns [-3,1,4,3,4]

Nothing equals 2: -3 and 1 go first, then 4, 3, 4 keep their order.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems