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
def circularPermutation ( n , start ) : NEW_LINE INDENT res = [ 0 ] NEW_LINE ret = [ ] NEW_LINE index , add = - 1 , 1 NEW_LINE for k in range ( n ) : NEW_LINE INDENT add = 1 << k NEW_LINE for i in range ( len ( res ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( res [ i ] + add == start ) : NEW_LINE INDENT index = len ( res ) NEW_LINE DEDENT res . append ( res [ i ] + add ) NEW_LINE DEDENT add = 1 << k NEW_LINE DEDENT if ( start == 0 ) : NEW_LINE INDENT return res NEW_LINE DEDENT while ( len ( ret ) < len ( res ) ) : NEW_LINE INDENT ret . append ( res [ index ] ) NEW_LINE index = ( index + 1 ) % len ( res ) NEW_LINE DEDENT return ret NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , S = 2 , 3 NEW_LINE print ( circularPermutation ( N , S ) ) NEW_LINE DEDENT