


For N = 3 one valid array is [3,1,2,1,3,2].
The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.
The first and the only line of each test case contains a single integer ‘N’.
The checker will print “Valid” if the returned permutation is valid and follows all the conditions, otherwise, it will print “Invalid”. If an empty array is returned, the checker will print -1.
Print the output of each test case in a new line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1<= T <= 5
1 <= N <= 8
where ’T’ is the number of test cases and ‘N’ is the given integer.
Time Limit: 1 sec
The idea is to make an array with exactly two occurrences of each element from 1 to N. Then we will generate all possible permutations of this array and check if any permutation is valid or not.
The steps are as follows:
This approach is similar to the previous approach but the idea is to carry on the recursion forward only if there are exactly k number of elements between both the occurrences of number ‘k’ (1<=k<=N). This way we can eliminate some function calls and reduce the time complexity.