Check if given number contains only “ 01 ” and “ 10 ” as substring in its binary representation | Python3 program to implement the above approach ; Function to generate all numbers having "01" and "10" as a substring ; Insert 2 and 5 ; Iterate till x is 10 ^ 15 ; Multiply x by 2 ; Update x as x * 2 + 1 ; Function to check if binary representation of N contains only "01" and "10" as substring ; Function Call to generate all such numbers ; Check if a number N exists in Ans [ ] or not ; If the number exists ; Driver Code
Ans = [ ] NEW_LINE def populateNumber ( ) : NEW_LINE INDENT Ans . append ( 2 ) NEW_LINE Ans . append ( 5 ) NEW_LINE x = 5 NEW_LINE while ( x < 1000000000001 ) : NEW_LINE INDENT x *= 2 NEW_LINE Ans . append ( x ) NEW_LINE x = x * 2 + 1 NEW_LINE Ans . append ( x ) NEW_LINE DEDENT DEDENT def checkString ( N ) : NEW_LINE INDENT populateNumber ( ) NEW_LINE for it in Ans : NEW_LINE INDENT if ( it == N ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT DEDENT return " No " NEW_LINE DEDENT N = 5 NEW_LINE print ( checkString ( N ) ) NEW_LINE