Construct binary palindrome by repeated appending and trimming | function to apply DFS ; set the parent marked ; if the node has not been visited set it and its children marked ; link which digits must be equal ; connect the two indices ; set everything connected to first character as 1 ; Driver Code
def dfs ( parent , ans , connectchars ) : NEW_LINE INDENT ans [ parent ] = 1 NEW_LINE for i in range ( len ( connectchars [ parent ] ) ) : NEW_LINE INDENT if ( not ans [ connectchars [ parent ] [ i ] ] ) : NEW_LINE INDENT dfs ( connectchars [ parent ] [ i ] , ans , connectchars ) NEW_LINE DEDENT DEDENT DEDENT def printBinaryPalindrome ( n , k ) : NEW_LINE INDENT arr = [ 0 ] * n NEW_LINE ans = [ 0 ] * n NEW_LINE connectchars = [ [ ] for i in range ( k ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] = i % k NEW_LINE DEDENT for i in range ( int ( n / 2 ) ) : NEW_LINE INDENT connectchars [ arr [ i ] ] . append ( arr [ n - i - 1 ] ) NEW_LINE connectchars [ arr [ n - i - 1 ] ] . append ( arr [ i ] ) NEW_LINE DEDENT dfs ( 0 , ans , connectchars ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( ans [ arr [ i ] ] , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE k = 4 NEW_LINE printBinaryPalindrome ( n , k ) NEW_LINE DEDENT