Construct the lexicographically largest valid sequence of length with distance constraints for each number.
Problem
Given an integer , build an array seq of length 2n - 1 using every integer from 1 to n subject to these rules:
- The number
1appears exactly once. - Each number
xwhere2 <= x <= nappears exactly twice. - For every
x >= 2, the two occurrences ofxmust be exactlyxindices apart.- If the first occurrence is at index
i, the second must be at indexi + x.
- If the first occurrence is at index
Among all valid arrays, return the lexicographically largest one.
A valid sequence is guaranteed to exist for the intended input range.
Input Format
- A single integer
n. - Construct an array of length
2n - 1satisfying the rules above.
Output Format
- Return the lexicographically largest valid sequence as an integer array.
Constraints
- The sequence length is
- For each , there must be exactly two occurrences separated by positions
- The value
1appears exactly once - A valid construction exists
Example 1
Input
n = 3
Output
[3,1,2,3,2]
Explanation
The sequence has length 5. 3 appears at indices 0 and 3, and 2 appears at indices 2 and 4. 1 appears once. This is the lexicographically largest valid sequence for n = 3.
Example 2
Input
n = 5
Output
[5,3,1,4,3,5,2,4,2]
Explanation
All numbers from 1 to 5 are used correctly. The two 5s are 5 apart, the two 4s are 4 apart, the two 3s are 3 apart, and the two 2s are 2 apart.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.