Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to construct the lexicographically largest valid sequence in Python
Suppose we have a number n, we have to find a sequence that satisfies all of the following rules ?
1 occurs once in the sequence.
Each number in between 2 and n occurs twice in the sequence.
For every i in range 2 to n, the distance between the two occurrences of i is exactly i.
The distance between two numbers in the sequence, a[i] and a[j], is |j - i|. We have to find the lexicographically largest sequence.
So, if the input is like n = 4, then the output will be [4, 2, 3, 2, 4, 3, 1]
Algorithm
To solve this, we will follow these steps ?
- Define a function backtrack() that takes elems, res, start := 0
- If size of elems <= 0, return True
- If start >= size of res, return False
- If res[start] is not -1, return backtrack(elems, res, start + 1)
- For each element in elems, try placing it at current position
- Calculate distance: 0 for number 1, otherwise the number itself
- If valid placement possible, place the number and recursively solve
- If recursive call fails, backtrack by removing the placement
Implementation
Let us see the following implementation to get better understanding ?
def backtrack(elems, res, start=0):
if len(elems) <= 0:
return True
if start >= len(res):
return False
if res[start] != -1:
return backtrack(elems, res, start + 1)
for i in range(len(elems)):
num = elems[i]
dist = 0 if num == 1 else num
if (start + dist) < len(res) and res[start + dist] == -1:
res[start] = num
res[start + dist] = num
elems.pop(i)
if not backtrack(elems, res, start):
res[start] = -1
res[start + dist] = -1
elems.insert(i, num)
continue
else:
return True
def solve(n):
elems = [i for i in range(n, 0, -1)]
res = [-1 for i in range(n * 2 - 1)]
backtrack(elems, res)
return res
# Test the solution
n = 4
result = solve(n)
print(f"For n = {n}, the lexicographically largest sequence is:")
print(result)
For n = 4, the lexicographically largest sequence is: [4, 2, 3, 2, 4, 3, 1]
How It Works
The algorithm uses backtracking to construct the sequence ?
- Lexicographically largest: We start with elements sorted in descending order [n, n-1, ..., 2, 1]
- Distance constraint: For number i, we place it at position start and start + i (or start + 0 for number 1)
- Backtracking: If placement leads to invalid solution, we undo and try next possibility
- Base cases: Return True when all elements placed, False when no valid placement possible
Example Walkthrough
For n = 4, we need to place [4, 3, 2, 1] in a sequence of length 7 ?
- Place 4 at positions 0 and 4: [4, _, _, _, 4, _, _]
- Place 2 at positions 1 and 3: [4, 2, _, 2, 4, _, _]
- Place 3 at positions 2 and 5: [4, 2, 3, 2, 4, 3, _]
- Place 1 at position 6: [4, 2, 3, 2, 4, 3, 1]
Conclusion
This backtracking solution efficiently constructs the lexicographically largest valid sequence by trying placements in descending order. The algorithm ensures all distance constraints are satisfied while maximizing the lexicographic value.
