Ctrl K

Low, High, Low

Medium
You get an integer array values. Rearrange its elements so that they alternate low and high: values[0] ≤ values[1] ≥ values[2] ≤ values[3] ≥ values[4] ≤ … Such an arrangement always exists, and usually there are several; any of them is accepted. Return the rearranged array. Sorting solves it in O(n log n). Can you do it in a single pass?
Example 1
values=[4, 2, 9, 7, 1] → [2, 9, 4, 7, 1]

For example [2, 9, 4, 7, 1]: 2 ≤ 9 ≥ 4 ≤ 7 ≥ 1. Any other arrangement with the same alternation is fine too, say [1, 7, 2, 9, 4].

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

The inequalities are not strict, so equal numbers already satisfy them and the array can stay as it is.

Example 3
values=[3, 8] → [3, 8]
Constraints
  • 1 ≤ len(values) ≤ 5·10⁴
  • 0 ≤ values[i] ≤ 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumStudios for Recording Sessions MediumStrictly Low, High, Low MediumFewest Meetings to Cancel MediumFewest Darts for the Balloons Medium
loading Python…
Input
values=[4, 2, 9, 7, 1]
Expected
[2, 9, 4, 7, 1]

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