> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Construct the Lexicographically Largest Valid

> Tested Python solution for LeetCode 1718 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 1718, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Backtracking](/catalog/topics/backtracking). [View on LeetCode](https://leetcode.com/problems/construct-the-lexicographically-largest-valid-sequence/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1718   # by problem number
lcpy gen -s construct_the_lexicographically_largest_valid_sequence   # by problem name
```

## Problem

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

* 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`.

### Examples

```
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.
```

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

### Constraints

* 1 \<= n \<= 20

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/construct_the_lexicographically_largest_valid_sequence/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/construct_the_lexicographically_largest_valid_sequence/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n!)
    # Space: O(n)
    def construct_distanced_sequence(self, n: int) -> list[int]:
        size = 2 * n - 1
        result = [0] * size
        used = [False] * (n + 1)

        def backtrack(index: int) -> bool:
            if index == size:
                return True
            if result[index] != 0:
                return backtrack(index + 1)
            for num in range(n, 0, -1):
                if used[num]:
                    continue
                second = index + num
                if num == 1:
                    result[index] = 1
                    used[1] = True
                    if backtrack(index + 1):
                        return True
                    result[index] = 0
                    used[1] = False
                elif second < size and result[second] == 0:
                    result[index] = result[second] = num
                    used[num] = True
                    if backtrack(index + 1):
                        return True
                    result[index] = result[second] = 0
                    used[num] = False
            return False

        backtrack(0)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n!) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.