Ctrl K

Two Piles by Larger Count

Hard
You are given an array values. Its numbers are dealt, in order, into two piles, a left one and a right one: - the first number goes to the left pile, the second to the right pile; - each following number x goes to the pile that holds **more** numbers strictly greater than x; - if both piles hold equally many such numbers, x goes to the pile that is currently smaller; - if the sizes are equal too, it goes to the left pile. Each number is appended to the end of its pile. Return the left pile followed by the right pile as a single array.
Example 1
values=[4, 6, 5, 1, 5] → [4, 6, 5, 1, 5]

Left pile [4], right pile [6]. For 5: 0 larger on the left, 1 on the right, so it goes right. For 1: 1 vs 2, right again. For 5: 0 vs 1, right. Result: [4] + [6, 5, 1, 5].

Example 2
values=[2, 2, 2, 2] → [2, 2, 2, 2]

Nothing is ever larger, so each number goes to the shorter pile, and to the left one on a tie: [2, 2] + [2, 2].

Example 3
values=[9, 3, 7, 8] → [9, 7, 8, 3]
Constraints
  • 3 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCity Block Outline HardSmaller Values to the Right HardWindow Sums Within a Band HardPairs With a Steep Drop HardA Dark Gap Between Street Lamps Hard
loading Python…
Input
values=[4, 6, 5, 1, 5]
Expected
[4, 6, 5, 1, 5]

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