Construct the Lexicographically Largest Valid Sequence - Problem
Construct the Lexicographically Largest Valid Sequence

You're tasked with creating a special sequence of numbers with very specific rules. Given an integer n, you need to construct a sequence where:

๐Ÿ”น The number 1 appears exactly once
๐Ÿ”น Each number from 2 to n appears exactly twice
๐Ÿ”น For any number i (where i โ‰ฅ 2), the distance between its two occurrences must be exactly i

The distance between two positions in the sequence is the absolute difference of their indices. For example, if number 3 appears at positions 2 and 5, the distance is |5 - 2| = 3.

Your goal is to find the lexicographically largest sequence that satisfies all these constraints. A sequence A is lexicographically larger than sequence B if, at the first position where they differ, A has a larger number.

Example: For n=3, the sequence [3,1,2,3,2] is valid because:
- Number 1 appears once at position 1
- Number 2 appears at positions 2 and 4 (distance = 2)
- Number 3 appears at positions 0 and 3 (distance = 3)

Input & Output

example_1.py โ€” Small case
$ Input: n = 3
โ€บ Output: [3,1,2,3,2]
๐Ÿ’ก Note: For n=3: Number 1 appears once at position 1. Number 2 appears at positions 2 and 4 (distance = 2). Number 3 appears at positions 0 and 3 (distance = 3). This is the lexicographically largest valid sequence.
example_2.py โ€” Minimal case
$ Input: n = 1
โ€บ Output: [1]
๐Ÿ’ก Note: For n=1: Only number 1 exists, so the sequence is simply [1].
example_3.py โ€” Medium case
$ Input: n = 4
โ€บ Output: [4,1,3,2,4,2,3]
๐Ÿ’ก Note: For n=4: The sequence satisfies all distance constraints. Number 1 appears once, and each number from 2 to 4 appears twice with the correct distance between occurrences.

Visualization

Tap to expand
Theater Seating for VIP GuestsGuest 3 (Couple): Needs 3 seats between them333 seats apartGuest 2 (Couple): Needs 2 seats between them32232 seats apartGuest 1 (Loner): Needs only 1 seat32123โœ“ Final Arrangement: [3,2,1,2,3]Key Strategy: Start with highest-priority guests (largest numbers) firstThis ensures lexicographically largest arrangement
Understanding the Visualization
1
Start with Highest VIP
Try to seat the most important guest (number n) first
2
Place Couples
For couples, place both seats with exact distance requirement
3
Place Loner
Guest 1 gets a single seat wherever it fits
4
Backtrack if Stuck
If no arrangement works, try different positions
Key Takeaway
๐ŸŽฏ Key Insight: By trying to place the largest numbers first at each position, we naturally construct the lexicographically largest valid sequence while using backtracking to handle constraint violations efficiently.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O((2n-1)!)

We generate all permutations of 2n-1 elements (1 appears once, 2 to n appear twice)

n
2n
โœ“ Linear Growth
Space Complexity
O(2n-1)

Space for storing the sequence and recursion stack

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 20
  • The sequence length will be 2n - 1
  • It is guaranteed that a solution always exists
Asked in
Google 42 Amazon 28 Meta 24 Microsoft 18
28.2K Views
Medium Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen