Check if the binary representation of a number has equal number of 0s and 1s in blocks
Last Updated :
21 Nov, 2022
Given an integer N, the task is to check if its equivalent binary number has an equal frequency of consecutive blocks of 0 and 1. Note that 0 and a number with all 1s are not considered to have a number of blocks of 0s and 1s.
Examples:
Input: N = 5
Output: Yes
Equivalent binary number of 5 is 101.
The first block is of 1 with length 1, the second block is of 0 with length 1
and the third block is of 1 is also of length 1. So, all blocks of 0 and 1 have an equal frequency which is 1.
Input: N = 51
Output: Yes
Equivalent binary number of 51 is 110011.
Input: 8
Output: No
Input: 7
Output: No
Simple Approach: First convert the integer to its equivalent binary number. Traverse binary string from left to right, for each block of 1 or 0, find its length and add it in a set. If length of set is 1, print “Yes” else print “No”.
C++
#include <bits/stdc++.h>
using namespace std;
void hasEqualBlockFrequency( int N)
{
string S = bitset<3> (N).to_string();
set< int > p;
int c = 1;
for ( int i = 0; i < S.length(); i++)
{
if (S[i] == S[i + 1])
c += 1;
else
{
p.insert(c);
c = 1;
}
p.insert(c);
}
if (p.size() == 1)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
int main()
{
int N = 5;
hasEqualBlockFrequency(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void hasEqualBlockFrequency( int N)
{
String S = Integer.toString(N, 2 );
HashSet<Integer> p = new HashSet<Integer>();
int c = 1 ;
for ( int i = 0 ; i < S.length() - 1 ; i++)
{
if (S.charAt(i) == S.charAt(i + 1 ))
c += 1 ;
else
{
p.add(c);
c = 1 ;
}
p.add(c);
}
if (p.size() == 1 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String []args)
{
int N = 5 ;
hasEqualBlockFrequency(N);
}
}
|
Python3
def hasEqualBlockFrequency(N):
S = bin (N).replace( "0b" , "")
p = set ()
c = 1
for i in range ( len (S) - 1 ):
if (S[i] = = S[i + 1 ]):
c + = 1
else :
p.add(c)
c = 1
p.add(c)
if ( len (p) = = 1 ):
print ( "Yes" )
else :
print ( "No" )
N = 5
hasEqualBlockFrequency(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void hasEqualBlockFrequency( int N)
{
string S = Convert.ToString(N, 2);
HashSet< int > p = new HashSet< int >();
int c = 1;
for ( int i = 0; i < S.Length - 1; i++)
{
if (S[i] == S[i + 1])
c += 1;
else
{
p.Add(c);
c = 1;
}
p.Add(c);
}
if (p.Count == 1)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
static void Main()
{
int N = 5;
hasEqualBlockFrequency(N);
}
}
|
Javascript
<script>
function hasEqualBlockFrequency(N)
{
let S = N.toString(2);
let p = new Set();
let c = 1;
for (let i = 0; i < S.length - 1; i++)
{
if (S[i] == S[i + 1])
c += 1;
else
{
p.add(c);
c = 1;
}
p.add(c);
}
if (p.size == 1)
document.write( "Yes" );
else
document.write( "No" );
}
let N = 5;
hasEqualBlockFrequency(N);
</script>
|
Optimized Solution: We traverse from last bit. We first count number of same bits in the last block. We then traverse through all bits, for every block, we count number of same bits and if this count is not same as first count, we return false. If all blocks have same count, we return true.
C++
#include <iostream>
using namespace std;
bool isEqualBlock( int n)
{
int first_bit = n % 2;
int first_count = 1;
n = n / 2;
while (n % 2 == first_bit && n > 0) {
n = n / 2;
first_count++;
}
if (n == 0)
return false ;
while (n > 0) {
int first_bit = n % 2;
int curr_count = 1;
n = n / 2;
while (n % 2 == first_bit) {
n = n / 2;
curr_count++;
}
if (curr_count != first_count)
return false ;
}
return true ;
}
int main()
{
int n = 51;
if (isEqualBlock(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean isEqualBlock( int n)
{
int first_bit = n % 2 ;
int first_count = 1 ;
n = n / 2 ;
while (n % 2 == first_bit && n > 0 )
{
n = n / 2 ;
first_count++;
}
if (n == 0 )
return false ;
while (n > 0 )
{
first_bit = n % 2 ;
int curr_count = 1 ;
n = n / 2 ;
while (n % 2 == first_bit)
{
n = n / 2 ;
curr_count++;
}
if (curr_count != first_count)
return false ;
}
return true ;
}
public static void main (String[] args)
{
int n = 51 ;
if (isEqualBlock(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def isEqualBlock(n):
first_bit = n % 2
first_count = 1
n = n / / 2
while n % 2 = = first_bit and n > 0 :
n = n / / 2
first_count + = 1
if n = = 0 :
return False
while n > 0 :
first_bit = n % 2
curr_count = 1
n = n / / 2
while n % 2 = = first_bit:
n = n / / 2
curr_count + = 1
if curr_count ! = first_count:
return False
return True
if __name__ = = "__main__" :
n = 51
if isEqualBlock(n):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isEqualBlock( int n)
{
int first_bit = n % 2;
int first_count = 1;
n = n / 2;
while (n % 2 == first_bit && n > 0)
{
n = n / 2;
first_count++;
}
if (n == 0)
return false ;
while (n > 0)
{
first_bit = n % 2;
int curr_count = 1;
n = n / 2;
while (n % 2 == first_bit)
{
n = n / 2;
curr_count++;
}
if (curr_count != first_count)
return false ;
}
return true ;
}
public static void Main ()
{
int n = 51;
if (isEqualBlock(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isEqualBlock( $n )
{
$first_bit = $n % 2;
$first_count = 1;
$n = (int)( $n / 2);
while ( $n % 2 == $first_bit && $n > 0)
{
$n = (int)( $n / 2);
$first_count ++;
}
if ( $n == 0)
return false;
while ( $n > 0)
{
$first_bit = $n % 2;
$curr_count = 1;
$n = (int)( $n / 2);
while ( $n % 2 == $first_bit )
{
$n = (int)( $n / 2);
$curr_count ++;
}
if ( $curr_count != $first_count )
return false;
}
return true;
}
$n = 51;
if (isEqualBlock( $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isEqualBlock(n)
{
var first_bit = n % 2;
var first_count = 1;
n = n / 2;
while (n % 2 == first_bit && n > 0)
{
n = n / 2;
first_count++;
}
if (n == 0)
return false ;
while (n > 0)
{
first_bit = n % 2;
var curr_count = 1;
n = n / 2;
while (n % 2 == first_bit)
{
n = n / 2;
curr_count++;
}
if (curr_count != first_count)
return false ;
}
return true ;
}
var n = 51;
if (isEqualBlock(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time complexity: O(log(n))
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...