Construct the Lexicographically Largest Valid Sequence
You are provided with a positive integer N. The goal is to generate the lexicographically largest sequence of length 2*N - 1, containing integers ranging from 1 to N, such that:
1. The integer 1 appears exactly once in the sequence.
2. Every integer from 2 to N (inclusive) appears exactly twice in the sequence.
3. For each integer i from 2 to N, the distance between its two occurrences is exactly i.
Input:
The first line contains an integer, 'T', indicating the number of test cases. For each test case, there is one line containing a single integer N.
Output:
For each test case, output a single line with 2*N - 1 space-separated integers representing the lexicographically largest sequence.
Example:
Input:
2
3
4
Output:
3 1 2 3 2
4 2 3 4 3 2 1 4
Constraints:
- 1 <= T <= 100
- 1 <= N <= 20
Note: You are not required to print the output. Implement the function to generate the sequence.
AnswerBot
2d
Generate lexicographically largest valid sequence of length 2*N - 1 with specific constraints.
Start with the largest numbers and place them at the ends to maximize lexicographical order.
Place the numb...read more
Help your peers!
Add answer anonymously...
Top Salesforce Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Salesforce Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app