Ctrl K

A Garland Without Smooth Steps

Medium
A designer is stringing a garland of bulbs of different brightness: brightness[i] is the brightness of bulb i, and all values are distinct. The designer dislikes "smooth steps": spots where a bulb's brightness is exactly the average of its two neighbours. The bulbs at either end have only one neighbour, so the rule does not apply to them. There is a simple way to hang the bulbs with no smooth steps. Sort the brightness values in ascending order. The first ⌈n/2⌉ values form the dim half and the rest form the bright half. Hang the bulbs alternating between the halves: the dimmest of the dim half, the dimmest of the bright half, the second of the dim half, the second of the bright half, and so on. Return the garland built exactly this way. Think about why it never contains a smooth step.
Example 1
brightness=[7, 1, 4, 9, 3] → [1, 7, 3, 9, 4]

Sorted: 1, 3, 4, 7, 9. The dim half is 1, 3, 4 and the bright half is 7, 9. Alternating them gives [1, 7, 3, 9, 4]. No bulb equals the average of its neighbours: for instance, (1 + 3) / 2 = 2 ≠ 7.

Example 2
brightness=[20, 50, 10, 40] → [10, 40, 20, 50]

Sorted: 10, 20, 40, 50. The halves are 10, 20 and 40, 50, so the result is [10, 40, 20, 50].

Example 3
brightness=[2, 0, 1] → [0, 2, 1]
Constraints
  • 3 ≤ len(brightness) ≤ 10⁵
  • 0 ≤ brightness[i] ≤ 10⁵
  • All values in brightness are distinct
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 MediumLow, High, Low MediumStrictly Low, High, Low MediumFewest Meetings to Cancel Medium
loading Python…
Input
brightness=[7, 1, 4, 9, 3]
Expected
[1, 7, 3, 9, 4]

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