Check if given number contains only “ 01 ” and “ 10 ” as substring in its binary representation | javascript 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
var N = 200000 ; var Ans = Array . from ( { length : N } , ( _ , i ) => 0 ) ; var index = 0 ; function populateNumber ( ) { Ans [ index ++ ] = ( 2 ) ; Ans [ index ++ ] = ( 5 ) ; var x = 5 ; var inf = 1000000000001 ; while ( x < inf ) { x *= 2 ; Ans [ index ++ ] = ( x ) ; x = x * 2 + 1 ; Ans [ index ++ ] = ( x ) ; } } function checkString ( N ) { populateNumber ( ) ; for ( i = 0 ; i < index ; i ++ ) { if ( Ans [ i ] == N ) { document . write ( " " ) ; return ; } } document . write ( " " ) ; } N = 5 ; checkString ( N ) ;