 Open in App
Not now

# Check if given number contains only “01” and “10” as substring in its binary representation

• Last Updated : 10 Aug, 2021

Given a number N, the task is to check if the binary representation of the number N has only “01” and “10” as a substring or not. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:

Input: N = 5
Output: Yes
Explanation:
(5)10 is (101)2 which contains only “01” and “10” as substring.
Input: N = 11
Output: No
Explanation:
(11)10 is (1011)2 which contains only “11” a substring.

Approach: The given problem can be solved using the following observations:

• Since the substring must contain “01” and “10”. Therefore, the binary representation contains 1 and 0 alternately.
• For binary representations containing 0 and 1 alternately, the following pattern can be observed:

2, 5, 10, 21, …

• The above pattern is of the form 2 * x and (4 * x + 1) such that the last term of the series is x.

From the above observation, the idea is to generate the given series using the above pattern and check if the given number exists in the pattern or not. If found to be true, then print “Yes”. Otherwise, print “No”.
Below is the implementation for the above approach:

## C++

 `// C++ program for the above approach`   `#include "bits/stdc++.h"` `using` `namespace` `std;`   `vector<``int``> Ans;`   `// Function to generate all numbers` `// having "01" and "10" as a substring` `void` `populateNumber()` `{` `    ``// Insert 2 and 5` `    ``Ans.push_back(2);` `    ``Ans.push_back(5);`   `    ``long` `long` `int` `x = 5;`   `    ``// Iterate till x is 10 ^ 15` `    ``while` `(x < 1000000000001) {`   `        ``// Multiply x by 2` `        ``x *= 2;` `        ``Ans.push_back(x);`   `        ``// Update x as x*2  + 1` `        ``x = x * 2 + 1;`   `        ``Ans.push_back(x);` `    ``}` `}`   `// Function to check if binary representation` `// of N contains only "01" and "10" as substring` `string checkString(``long` `long` `int` `N)` `{` `    ``// Function Call to generate all` `    ``// such numbers` `    ``populateNumber();`   `    ``// Check if a number N` `    ``// exists in Ans[] or not` `    ``for` `(``auto``& it : Ans) {`   `        ``// If the number exists` `        ``if` `(it == N) {` `            ``return` `"Yes"``;` `        ``}` `    ``}`   `    ``// Otherwise` `    ``return` `"No"``;` `}`   `// Driver Code` `int` `main()` `{` `    ``long` `long` `int` `N = 5;`   `    ``// Function Call` `    ``cout << checkString(N);`   `    ``return` `0;` `}`

## Java

 `// Java Program to implement` `// the above approach` `import` `java.util.*; ` `class` `GFG ` `{` `  ``static` `int` `N = ``200000``;`   `  ``static` `long` `Ans[] = ``new` `long` `[``200000``];` `  ``static` `int` `index = ``0``;`   `  ``// Function to generate all numbers` `  ``// having "01" and "10" as a substring` `  ``static` `void` `populateNumber()` `  ``{` `    ``// Insert 2 and 5` `    ``Ans[index++] = (``2``);` `    ``Ans[index++] = (``5``);` `    ``long` `x = ``5``;` `    ``long` `inf = 1000000000001L;`   `    ``// Iterate till x is 10 ^ 15` `    ``while` `(x < inf)` `    ``{`   `      ``// Multiply x by 2` `      ``x *= ``2``;` `      ``Ans[index++] = (x);`   `      ``// Update x as x*2  + 1` `      ``x = x * ``2` `+ ``1``;`   `      ``Ans[index++] = (x);` `    ``}` `  ``}` `  ``// Function to check if binary representation` `  ``// of N contains only "01" and "10" as substring` `  ``static` `void` `checkString(``int` `N)` `  ``{` `    ``// Function Call to generate all` `    ``// such numbers` `    ``populateNumber();`   `    ``// Check if a number N` `    ``// exists in Ans[] or not` `    ``for` `(``int` `i = ``0``; i < index; i++) ` `    ``{`   `      ``// If the number exists` `      ``if` `(Ans[i] == N) ` `      ``{` `        ``System.out.println(``"YES"``);` `        ``return``;` `      ``}` `    ``}` `    ``System.out.println(``"NO"``);`   `  ``}`     `  ``// Driver Code` `  ``public` `static` `void` `main(String[] args) ` `  ``{` `    ``N = ``5``;` `    ``checkString(N);` `  ``}` `}`   `// This code is contributed by amreshkumar3.`

## Python3

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

## C#

 `// C# Program to implement` `// the above approach` `using` `System;` `class` `GFG ` `{` `  ``static` `int` `N = 200000;` `  ``static` `long` `[]Ans = ``new` `long` `;` `  ``static` `int` `index = 0;`   `  ``// Function to generate all numbers` `  ``// having "01" and "10" as a substring` `  ``static` `void` `populateNumber()` `  ``{` `    `  `    ``// Insert 2 and 5` `    ``Ans[index++] = (2);` `    ``Ans[index++] = (5);` `    ``long` `x = 5;` `    ``long` `inf = 1000000000001L;`   `    ``// Iterate till x is 10 ^ 15` `    ``while` `(x < inf)` `    ``{`   `      ``// Multiply x by 2` `      ``x *= 2;` `      ``Ans[index++] = (x);`   `      ``// Update x as x*2  + 1` `      ``x = x * 2 + 1;` `      ``Ans[index++] = (x);` `    ``}` `  ``}` `  `  `  ``// Function to check if binary representation` `  ``// of N contains only "01" and "10" as substring` `  ``static` `void` `checkString(``int` `N)` `  ``{` `    `  `    ``// Function Call to generate all` `    ``// such numbers` `    ``populateNumber();`   `    ``// Check if a number N` `    ``// exists in Ans[] or not` `    ``for` `(``int` `i = 0; i < index; i++) ` `    ``{`   `      ``// If the number exists` `      ``if` `(Ans[i] == N) ` `      ``{` `        ``Console.WriteLine(``"YES"``);` `        ``return``;` `      ``}` `    ``}` `    ``Console.WriteLine(``"NO"``);` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `Main(String[] args) ` `  ``{` `    ``N = 5;` `    ``checkString(N);` `  ``}` `}`   `// This code is contributed by shikhasingrajput`

## Javascript

 ``

Output:

`Yes`

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles