Problem library

Bracket Order

Medium
recursionarrays

A single-elimination bracket for n players (where n is a power of two, at least 2) is seeded so that, if the higher seed always wins, seed 1 and seed 2 can only meet in the final, seeds 1 to 4 can only meet in the semifinals, and so on.

The bracket for 2 players is [1, 2]. To build the bracket for 2m players from the bracket for m players, replace every seed s with the pair s, 2m + 1 - s.

Return the bracket, read top to bottom, as a list of seeds. Adjacent entries (positions 0 and 1, 2 and 3, ...) play each other in round one.

Examples

Input: n = 4
Output: [1,4,2,3]
Input: n = 8
Output: [1,8,4,5,2,7,3,6]