<p>An <strong>n-bit gray code sequence</strong> is a sequence of <code>2<sup>n</sup></code> integers where:</p>

<ul>
	<li>Every integer is in the <strong>inclusive</strong> range <code>[0, 2<sup>n</sup> - 1]</code>,</li>
	<li>The first integer is <code>0</code>,</li>
	<li>An integer appears <strong>no more than once</strong> in the sequence,</li>
	<li>The binary representation of every pair of <strong>adjacent</strong> integers differs by <strong>exactly one bit</strong>, and</li>
	<li>The binary representation of the <strong>first</strong> and <strong>last</strong> integers differs by <strong>exactly one bit</strong>.</li>
</ul>

<p>Given an integer <code>n</code>, return <em>any valid <strong>n-bit gray code sequence</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> [0,1,3,2]
<strong>Explanation:</strong>
The binary representation of [0,1,3,2] is [00,01,11,10].
- 0<u>0</u> and 0<u>1</u> differ by one bit
- <u>0</u>1 and <u>1</u>1 differ by one bit
- 1<u>1</u> and 1<u>0</u> differ by one bit
- <u>1</u>0 and <u>0</u>0 differ by one bit
[0,2,3,1] is also a valid gray code sequence, whose binary representation is [00,10,11,01].
- <u>0</u>0 and <u>1</u>0 differ by one bit
- 1<u>0</u> and 1<u>1</u> differ by one bit
- <u>1</u>1 and <u>0</u>1 differ by one bit
- 0<u>1</u> and 0<u>0</u> differ by one bit
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> [0,1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 16</code></li>
</ul>

class Solution {
public:

   int binpow(int a, int b){
        if(b == 0)return 1;
        int v = binpow(a, b/2);
        if(b&1)return a*v*v;
        else return v*v;
   }

   //state - which elements are taken
   vector<int>numbers;
   vector<int>sol, ans;

    bool done = false;
    int N;
    
    bool check(int level, int val){
        if(level < N-1){
            return true;
        }
        if(level == N-1){
            int prev_value = ans[0];
             int xorValue = prev_value ^ val;
             int a = log2(xorValue);
            int  b = binpow(2, a);
            if(b != xorValue) return false;
        }
        return true;
        
    }


    void rec(int level, int n){
        if(done) return;

        if(level == N){
            done = true;
            sol = ans;
            return;
        }
        if(level == 0){
            //you can choose any number
            for(int i = 0; i < N; i++){
                ans.push_back(i);
                numbers[i] = 1;
                rec(1, n);
                ans.pop_back();
                numbers[i] = 0;   
            }
            return;
        }
       //choice check only n numbers
       for(int i = 0; i < n; i++){
           int val = ans[level-1]^(1 << i);
           if(check(level, val) && !numbers[val]){
               //place
               ans.push_back(val);
               numbers[val] = 1;
               //move
               rec(level+1, n);
               //unplace
               ans.pop_back();
               numbers[val] = 0;
           }

       }
    }
    vector<int> grayCode(int n) {
        N = binpow(2, n);
        numbers.assign(N, 0);
        rec(0, n);
        return sol;
        
    }
};
37381