Generate a circular permutation with number of mismatching bits between pairs of adjacent elements exactly 1 | C ++ program for the above approach ; Function to find the permutation of integers from a given range such that number of mismatching bits between pairs of adjacent elements is 1 ; Initialize an arrayList to store the resultant permutation ; Store the index of rotation ; Iterate over the range [ 0 , N - 1 ] ; Traverse all the array elements up to ( 2 ^ k ) - th index in reverse ; If current element is S ; Check if S is zero ; Rotate the array by index value to the left ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > circularPermutation ( int n , int start ) { vector < int > res = { 0 } ; vector < int > ret ; int index = -1 ; for ( int k = 0 , add = 1 << k ; k < n ; k ++ , add = 1 << k ) { for ( int i = res . size ( ) - 1 ; i >= 0 ; i -- ) { if ( res [ i ] + add == start ) index = res . size ( ) ; res . push_back ( res [ i ] + add ) ; } } if ( start == 0 ) return res ; while ( ret . size ( ) < res . size ( ) ) { ret . push_back ( res [ index ] ) ; index = ( index + 1 ) % res . size ( ) ; } return ret ; } int main ( ) { int N = 2 , S = 3 ; vector < int > print = circularPermutation ( N , S ) ; cout << " [ " ; for ( int i = 0 ; i < print . size ( ) - 1 ; i ++ ) { cout << print [ i ] << " , ▁ " ; } cout << print [ print . size ( ) - 1 ] << " ] " ; return 0 ; }