Given 2 integers n and start. Your task is return any permutation p&nbsp;of (0,1,2.....,2^n -1) such that :


	p[0] = start
	p[i] and p[i+1]&nbsp;differ by only one bit in their binary representation.
	p[0] and p[2^n -1]&nbsp;must also differ by only one bit in their binary representation.


&nbsp;
Example 1:

Input: n = 2, start = 3
Output: [3,2,0,1]
Explanation: The binary representation of the permutation is (11,10,00,01). 
All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2]


Example 2:

Input: n = 3, start = 2
Output: [2,6,7,5,4,0,1,3]
Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).


&nbsp;
Constraints:


	1 &lt;= n &lt;= 16
	0 &lt;= start&nbsp;&lt;&nbsp;2 ^ n

class Solution:
    def circularPermutation(self, n: int, start: int) -> List[int]:
        gray_code = [x ^ (x >> 1) for x in range(2 ** n)]
        start_i = gray_code.index(start)
        return gray_code[start_i:] + gray_code[:start_i]
class Solution {
    public List<Integer> circularPermutation(int n, int start) {
        List<Integer> l = new ArrayList<Integer>();
        int i=0;
        int len = (int)Math.pow(2,n);
        int[] arr = new int[len];
        while(i<len){
            arr[i]=(i)^(i/2);
            i++;
        }
        
        i=0;
        while(arr[i]!=start)i++;
        while(i<arr.length){
            l.add(arr[i]);
            i++;
        }
        i=0;
        while(i<arr.length && arr[i]!=start){
            l.add(arr[i]);
            i++;
        }
        return l;
    }
}
class Solution {
public:
    vector<string> get_val(int n)
    {
        if(n==1)return {"0","1"};
        vector<string> v = get_val(n-1);
        vector<string> ans;
        for(int i = 0;i<v.size();i++)
        {
            ans.push_back("0" + v[i]);
        }

        for(int i = v.size()-1;i>=0;i--)
        {
            ans.push_back("1" + v[i]);
        }
        return ans;
    }

    vector<int> solve(int n)
    {
        vector<string> v = get_val(n);
        vector<int> ans;
        for(int i = 0;i<v.size();i++)
        {
            string s = v[i];
            int x = 0;
            for(int j = 0;j<s.size();j++)
            {
                x = x*2 + s[j]-'0';
            }
            ans.push_back(x);
        }
        return ans;
    }

    vector<int> circularPermutation(int n, int start) {

        vector<int> v = solve(n);
        int ind;
        for(int i = 0;i<v.size();i++)
        {
            if(v[i]==start)
            {
                ind = i;
                break;
            }
        }

        vector<int> ans;
        for(int i = ind;i<v.size();i++)
        {
            ans.push_back(v[i]);
        }
        for(int i = 0;i<ind;i++)
        {
            ans.push_back(v[i]);
        }
        return ans;
    }
};
var circularPermutation = function(n, start) {
    const grayCodes = [];

    let startIdx = -1;

    for (let i = 0; i <= 2**n - 1; i++) {
        grayCodes[i] = i ^ (i >> 1);

        if (grayCodes[i] == start) startIdx = i;
    }

    const res = [];

    for (let i = 0; i <= 2**n - 1; i++) {
        res[i] = grayCodes[startIdx];

        startIdx++;

        if (startIdx == grayCodes.length) startIdx = 0;
    }

    return res;
};
Circular Permutation in Binary Representation