#1718

Construct the Lexicographically Largest Valid Sequence

pupil · 535 · lc medium +28 · verified · 72.8% accepted · 1,156 likes · top 82%

Description

Given an integer n, build a sequence with elements in [1, n] where 1 appears exactly once, each integer from 2 to n appears exactly twice, and for every i in [2, n] the two occurrences are exactly i positions apart.

Return the lexicographically largest valid sequence. A solution is guaranteed to exist.

Example 1:

Input: n = 3
Output: [3,1,2,3,2]
Explanation: [2,3,2,1,3] is also a valid sequence, but [3,1,2,3,2] is the lexicographically largest valid sequence.

Example 2:

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

Code

1
2
3