Find Permutation Problem Statement

Given an integer N, determine an array of size 2 * N that satisfies the following conditions:

  1. Each number from 1 to N appears exactly twice in the array.
  2. The distance between the second and first occurrence of each number is equal to the number itself.

If no valid array can be constructed, return an empty array.

Example:

Input:
N = 3
Output:
[3, 1, 2, 1, 3, 2]

Constraints:

  • 1 ≤ T ≤ 5
  • 1 ≤ N ≤ 8
  • Time Limit: 1 second

Input:

The input starts with an integer 'T' (the number of test cases).
For each test case, there is one line containing a single integer 'N'.

Output:

The output will indicate "Valid" if the returned permutation meets all the conditions, otherwise "Invalid". If an empty array is returned, the output will be -1.
Each test case result should be on a new line.
Note:
You are not required to print anything; implementation of the function suffices, as the result verification is handled automatically.
Be the first one to answer
Add answer anonymously...
JPMorgan Chase & Co. Machine Learning Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter