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 ; given number
def allCombinationsRec ( arr , elem , n ) : NEW_LINE INDENT if ( elem > n ) : NEW_LINE INDENT for i in ( arr ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE return NEW_LINE DEDENT for i in range ( 0 , 2 * n ) : NEW_LINE INDENT if ( arr [ i ] == - 1 and ( i + elem + 1 ) < 2 * n and arr [ i + elem + 1 ] == - 1 ) : NEW_LINE INDENT arr [ i ] = elem NEW_LINE arr [ i + elem + 1 ] = elem NEW_LINE allCombinationsRec ( arr , elem + 1 , n ) NEW_LINE arr [ i ] = - 1 NEW_LINE arr [ i + elem + 1 ] = - 1 NEW_LINE DEDENT DEDENT DEDENT def allCombinations ( n ) : NEW_LINE INDENT arr = [ - 1 ] * ( 2 * n ) NEW_LINE elem = 1 NEW_LINE allCombinationsRec ( arr , elem , n ) NEW_LINE DEDENT n = 3 NEW_LINE allCombinations ( n ) NEW_LINE