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
4mo

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!
Select
Add answer anonymously...

Salesforce Software Developer interview questions & answers

A Software Developer was asked Q. Pair with Given Sum in a Balanced BST Problem Statement You are given the ‘root’...read more
A Software Developer was asked Q. What is the difference between Early Binding and Late Binding in C++?
A Software Developer was asked Q. What is meant by multitasking and multithreading in operating systems?

Popular interview questions of Software Developer

A Software Developer was asked Q1. Pair with Given Sum in a Balanced BST Problem Statement You are given the ‘root’...read more
A Software Developer was asked Q2. What is the difference between Early Binding and Late Binding in C++?
A Software Developer was asked Q3. What is meant by multitasking and multithreading in operating systems?
Salesforce Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits