Beautiful Array

An array nums of length n is beautiful if:

  • nums is a permutation of the integers in the range [1, n].
  • For every 0 <= i < j < n, there is no index k with i < k < j where 2 * nums[k] == nums[i] + nums[j].

Given the integer n, return any beautiful array nums of length n. There will be at least one valid answer for the given n.

Example 1
Inputn = 4
Output[2,1,4,3]
The array is a permutation of [1, 4] and no middle element forms the average of two elements around it.
Example 2
Inputn = 5
Output[3,1,2,5,4]
The array is a permutation of [1, 5] and satisfies the required no-average condition for every pair of positions.

Constraints

  • 1 <= n <= 1000

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate