Ctrl K

No Average in Between

Medium
Arrange the numbers 1 to n (each exactly once) so that no number stands between two others whose average it is. In other words, for any positions i < k < j it must hold that 2 · a[k] ≠ a[i] + a[j]. There are many such orders, so return this one: - for n = 1 it is [1]; - otherwise all the odd numbers come first, then all the even ones. The odd numbers follow the order built for ⌈n/2⌉ with every x replaced by 2x − 1; the even numbers follow the order for ⌊n/2⌋ with every x replaced by 2x. Return that order. Think about why it really has no average in between.
Example 1
n=5 → [1, 5, 3, 2, 4]

The odd numbers 1, 3, 5 come from the order for 3, which is [1, 3, 2], by mapping x to 2x − 1: [1, 5, 3]. The even numbers 2, 4 come from the order for 2, [1, 2], by mapping x to 2x: [2, 4]. The result is [1, 5, 3, 2, 4].

Example 2
n=2 → [1, 2]

The odd 1 first, then the even 2.

Example 3
n=8 → [1, 5, 3, 7, 2, 6, 4, 8]
Constraints
  • 1 ≤ n ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNearest Points to the Base MediumOther Orders for the Same Search Tree HardNearest Pair of Sensors HardTurn a Square Clockwise MediumMost Profitable Streak Medium
loading Python…
Input
n=5
Expected
[1, 5, 3, 2, 4]

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