Check if given number contains only “ 01 ” and “ 10 ” as substring in its binary representation | C ++ program for 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
#include " bits / stdc + + . h " NEW_LINE using namespace std ; vector < int > Ans ; void populateNumber ( ) { Ans . push_back ( 2 ) ; Ans . push_back ( 5 ) ; long long int x = 5 ; while ( x < 1000000000001 ) { x *= 2 ; Ans . push_back ( x ) ; x = x * 2 + 1 ; Ans . push_back ( x ) ; } } string checkString ( long long int N ) { populateNumber ( ) ; for ( auto & it : Ans ) { if ( it == N ) { return " Yes " ; } } return " No " ; } int main ( ) { long long int N = 5 ; cout << checkString ( N ) ; return 0 ; }