Generate a circular permutation with number of mismatching bits between pairs of adjacent elements exactly 1 | 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
function circularPermutation ( n , start ) { var res = [ 0 ] ; var ret = [ ] ; var index = - 1 ; for ( var k = 0 , add = 1 << k ; k < n ; k ++ , add = 1 << k ) { for ( var i = res . length - 1 ; i >= 0 ; i -- ) { if ( res [ i ] + add == start ) index = res . length ; res . push ( res [ i ] + add ) ; } } if ( start == 0 ) return res ; while ( ret . length < res . length ) { ret . push ( res [ index ] ) ; index = ( index + 1 ) % res . length ; } return ret ; } var N = 2 , S = 3 ; var print = circularPermutation ( N , S ) ; document . write ( " " ) ; for ( var i = 0 ; i < print . length - 1 ; i ++ ) { document . write ( print [ i ] + " " ) ; } document . write ( print [ print . length - 1 ] + " " ) ;