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
using System ; using System . Collections . Generic ; public class GFG { public static List < int > circularPermutation ( int n , int start ) { List < int > res = new List < int > ( ) { 0 } ; List < int > ret = new List < int > ( ) ; int index = - 1 ; for ( int k = 0 , add = 1 << k ; k < n ; k ++ , add = 1 << k ) { for ( int i = res . Count - 1 ; i >= 0 ; i -- ) { if ( res [ i ] + add == start ) index = res . Count ; res . Add ( res [ i ] + add ) ; } } if ( start == 0 ) return res ; while ( ret . Count < res . Count ) { ret . Add ( res [ index ] ) ; index = ( index + 1 ) % res . Count ; } return ret ; } static public void Main ( ) { int N = 2 , S = 3 ; List < int > print = circularPermutation ( N , S ) ; Console . Write ( " [ " ) ; for ( int i = 0 ; i < print . Count - 1 ; i ++ ) { Console . Write ( print [ i ] + " , ▁ " ) ; } Console . Write ( print [ print . Count - 1 ] + " ] " ) ; } }