Update appNew update is available. Click here to update.

Circular Permutation In Binary Representation

Posted: 10 Mar, 2021
Difficulty: Moderate

PROBLEM STATEMENT

Try Problem

You are given the two integers 'N' and 'X'. Your task is to find an array 'P' such that:

'P' is a permutation of (0, 1, 2, ..., 2 ^ (N  - 1)).

The first element of the array 'P' is 'X', i.e., P[0] = X.

Adjacent elements of 'P' (i.e., P[i] and P[i + 1]) differ by only 1 bit in their binary representation.

The first and the last element (i.e., P[0] and P[2^N -1]) are also considered as adjacent elements.
Note:
For N = 2, [0,1,2,3], [0,2,3,1], [1,2,3,0] are some of the valid permutations but [0,0,1,2], [1,2,3,4], [1,1,1,3] are not.

It is guaranteed that an array 'P' always exits with the given requirements.
Input Format:
The first line contains an integer 'T', which denotes the number of test cases or queries to be run. Then, the 'T' test cases follow. 

The first line and only line of each test case contain 2 positive integers, 'N' and 'X', as described in the problem statement.
Output Format:
For each test case, print in a new line an array 'P' of size 2^N denoting a permutation of (0,1, ..., 2^N -1) with the given requirements, as described in the problem statement.

If there exist multiple permutations satisfying the above conditions, then you can print any.

Output for each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1 <= N <= 13
0 <= X < 2^N

Time Limit: 1 sec