Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-042

Partition Array by Pivot

Problem

Partition an array into three stable regions relative to a pivot value.

Elements smaller than pivot come first, then elements equal to pivot, then elements greater than pivot. Equal and remaining elements keep their relative order within each region.

Return the concatenated result.

Hint

Collect three lists and concatenate them in the order less, equal, greater.

Test cases

case 1
in: [[9,3,5,1,5,7],5]
expected: [3,1,5,5,9,7]
case 2
in: [[],3]
expected: []
case 3
in: [[2,2,2],2]
expected: [2,2,2]
case 4
in: [[-1,0,-5,3],0]
expected: [-1,-5,0,3]
Starter codePython
def partition_by_pivot(nums, pivot):
    # Your code here
    pass

4 test cases · run your code against them in the editor