Combinations where every element appears twice and distance between appearances is equal to the value | C # program to find all combinations where every element appears twice and distance between appearances is equal to the value ; Find all combinations that satisfies given constraints ; If all elements are filled , print the solution ; Try all possible combinations for element elem ; If position i and ( i + elem + 1 ) are not occupied in the vector ; Place elem at position i and ( i + elem + 1 ) ; Recurse for next element ; Backtrack ( remove elem from position i and ( i + elem + 1 ) ) ; Create a vector of double the size of given number with ; All its elements initialized by 1 ; Start from element 1 ; Driver Code ; Given number
using System ; using System . Collections . Generic ; class Test { static void allCombinationsRec ( List < int > arr , int elem , int n ) { if ( elem > n ) { foreach ( int i in arr ) Console . Write ( i + " ▁ " ) ; Console . WriteLine ( ) ; return ; } for ( int i = 0 ; i < 2 * n ; i ++ ) { if ( arr [ i ] == - 1 && ( i + elem + 1 ) < 2 * n && arr [ i + elem + 1 ] == - 1 ) { arr [ i ] = elem ; arr [ i + elem + 1 ] = elem ; allCombinationsRec ( arr , elem + 1 , n ) ; arr [ i ] = - 1 ; arr [ i + elem + 1 ] = - 1 ; } } } static void allCombinations ( int n ) { List < int > arr = new List < int > ( ) ; for ( int i = 0 ; i < 2 * n ; i ++ ) { arr . Add ( - 1 ) ; } int elem = 1 ; allCombinationsRec ( arr , elem , n ) ; } public static void Main ( string [ ] args ) { int n = 3 ; allCombinations ( n ) ; } }