Ctrl K

Sort Into Three Piles Around a Mark

Medium
You get an integer array values and a number mark that is guaranteed to appear in it. Rearrange the elements so that all numbers smaller than mark come first, then all numbers equal to mark, and finally all numbers greater than mark. Within the smaller group and within the greater group, keep the original order. Return the resulting array.
Example 1
values=[7, 2, 9, 5, 1, 5, 8], mark=5 → [2, 1, 5, 5, 7, 9, 8]

The smaller 2 and 1 come first in their original order, then both 5s, then 7, 9, 8, also in original order.

Example 2
values=[0, -4, 6, -1], mark=-1 → [-4, -1, 0, 6]

Only -4 is below -1; 0 and 6 are larger and keep their order.

Example 3
values=[3], mark=3 → [3]
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • -10⁶ ≤ values[i] ≤ 10⁶
  • mark appears in values
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMatch-Three Cascade MediumTwo Gardeners and Their Refills MediumSplit Text at the Marks MediumPlus, Minus, Plus MediumHiring From Both Ends of the Line Medium
loading Python…
Input
values=[7, 2, 9, 5, 1, 5, 8], mark=5
Expected
[2, 1, 5, 5, 7, 9, 8]

Run checks the first 3 tests in your browser. Submit checks all 104 tests on the server, hidden ones included.