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

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

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

<pre>
<strong>Input:</strong> n = 2, start = 3
<strong>Output:</strong> [3,2,0,1]
<strong>Explanation:</strong> 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]
</pre>

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

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

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

<ul>
	<li><code>1 &lt;= n &lt;= 16</code></li>
	<li><code>0 &lt;= start&nbsp;&lt;&nbsp;2 ^ n</code></li>
</ul>
class Solution {
public:
    int bit_diff(int a, int b) {
        int count = 0;
        while(a > 0 || b > 0) {
            if(a%2 != b%2) count++;
            a >>= 1;
            b >>= 1;
        }

        cout << a << " and " << b << " bit different count " << count << endl;
        return count;
    }

    bool helper(int n, int count, vector<int>& ans, int visited) {
        cout << "ans size " << ans.size() << " count " << count << endl;
        for(int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
        cout << endl;
        if(ans.size() == count) {
            return bit_diff(ans[0], ans.back()) == 1;
        }

        int old;
        for(int i = 0; i < n; i++) {
            if ((visited>>i)%2 == 1) continue;

            old = visited;
            visited |= 1<<i;

            int num = ans.back();
            if ((num>>i)%2 == 1) num &= ~(1<<i);
            else num |= (1<<i);

            cout << "push data " << num << endl;
            ans.push_back(num);
            if(helper(n, count, ans, visited)) return true;
            ans.pop_back();

            visited = old;
        }

        return false;
    }

    vector<int> circularPermutation(int n, int start) {
        vector<int> ans;
        for(int i = 0; i < 1<<n; i++) ans.emplace_back((i^(i>>1))^start);
        return ans;
    }
};
18900