Construct the Lexicographically Largest Valid Sequence - Problem

Given an integer n, find a sequence with elements in the range [1, n] that satisfies all of the following conditions:

  • The integer 1 occurs once in the sequence.
  • Each integer between 2 and n occurs twice in the sequence.
  • For every integer i between 2 and n, the distance between the two occurrences of i is exactly i.
  • The distance between two numbers on the sequence, a[i] and a[j], is the absolute difference of their indices, |j - i|.

Return the lexicographically largest sequence. It is guaranteed that under the given constraints, there is always a solution.

A sequence a is lexicographically larger than a sequence b (of the same length) if in the first position where a and b differ, sequence a has a number greater than the corresponding number in b. For example, [0,1,9,0] is lexicographically larger than [0,1,5,6] because the first position they differ is at the third number, and 9 is greater than 5.

Input & Output

Example 1 — Small Case
$ Input: n = 3
Output: [3,1,2,3,2]
💡 Note: For n=3, we need sequence with 1 once, 2 twice, 3 twice. Distance between two 2's is |3-1|=2 ✓, distance between two 3's is |4-0|=4≠3 ✗. Correct answer: [3,1,2,3,2] where 2's are at positions 2,4 (distance 2) and 3's are at positions 0,3 (distance 3).
Example 2 — Minimum Case
$ Input: n = 1
Output: [1]
💡 Note: For n=1, only need number 1 once. The sequence is simply [1].
Example 3 — Medium Case
$ Input: n = 4
Output: [4,1,3,1,2,4,2]
💡 Note: For n=4: 1 occurs once, 2,3,4 occur twice each. All distance constraints satisfied: 2's at distance 2, 3's at distance 3, 4's at distance 4. This is the lexicographically largest valid sequence.

Constraints

  • 1 ≤ n ≤ 20

Visualization

Tap to expand
Lexicographically Largest Valid Sequence INPUT Given integer n n = 3 Elements in range [1, n] 1 2 3 Constraints: - 1 appears once - 2 to n appear twice - Distance(i,i) = i - Lexicographically largest ALGORITHM (Greedy) 1 Start with largest n Try placing 3 at pos 0 2 Place second 3 At pos 0+3=3 3 Continue greedy Try next largest available 4 Backtrack if needed Try smaller number Building sequence: [3,_,_,3,_] [3,1,_,3,_] [3,1,2,3,_] [3,1,2,3,2] --> --> --> place 3 place 1 place 2 OK FINAL RESULT Lexicographically Largest Sequence 3 idx 0 1 idx 1 2 idx 2 3 idx 3 2 idx 4 [3, 1, 2, 3, 2] Verification: 1: appears once at idx 1 OK 2: idx 2,4 dist=2 OK 3: idx 0,3 dist=3 OK dist=3 Lexicographically Largest! Key Insight: Greedy approach: Always try the largest available number first at each position to ensure lexicographically largest result. If placing a number violates constraints (distance rule or overlap), backtrack and try smaller. For number i (except 1), both occurrences must be placed i positions apart. TutorialsPoint - Construct the Lexicographically Largest Valid Sequence | Greedy Approach
Asked in
Google 25 Facebook 18 Amazon 12
23.4K Views
Medium Frequency
~35 min Avg. Time
892 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