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++
#include "bits/stdc++.h"
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;
}
|
Java
import java.util.*;
class GFG
{
static int N = 200000 ;
static long Ans[] = new long [ 200000 ];
static int index = 0 ;
static void populateNumber()
{
Ans[index++] = ( 2 );
Ans[index++] = ( 5 );
long x = 5 ;
long inf = 1000000000001L;
while (x < inf)
{
x *= 2 ;
Ans[index++] = (x);
x = x * 2 + 1 ;
Ans[index++] = (x);
}
}
static void checkString( int N)
{
populateNumber();
for ( int i = 0 ; i < index; i++)
{
if (Ans[i] == N)
{
System.out.println( "YES" );
return ;
}
}
System.out.println( "NO" );
}
public static void main(String[] args)
{
N = 5 ;
checkString(N);
}
}
|
Python3
Ans = []
def populateNumber() :
Ans.append( 2 )
Ans.append( 5 )
x = 5
while (x < 1000000000001 ) :
x * = 2
Ans.append(x)
x = x * 2 + 1
Ans.append(x)
def checkString(N) :
populateNumber()
for it in Ans :
if (it = = N) :
return "Yes"
return "No"
N = 5
print (checkString(N))
|
C#
using System;
class GFG
{
static int N = 200000;
static long []Ans = new long [200000];
static int index = 0;
static void populateNumber()
{
Ans[index++] = (2);
Ans[index++] = (5);
long x = 5;
long inf = 1000000000001L;
while (x < inf)
{
x *= 2;
Ans[index++] = (x);
x = x * 2 + 1;
Ans[index++] = (x);
}
}
static void checkString( int N)
{
populateNumber();
for ( int i = 0; i < index; i++)
{
if (Ans[i] == N)
{
Console.WriteLine( "YES" );
return ;
}
}
Console.WriteLine( "NO" );
}
public static void Main(String[] args)
{
N = 5;
checkString(N);
}
}
|
Javascript
<script>
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( "YES" );
return ;
}
}
document.write( "NO" );
}
N = 5;
checkString(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...