Check if the binary representation of a number has equal number of 0s and 1s in blocks

Given an integer N, the task is to check if its equivalent binary number has the equal frequency of consecutive blocks of 0 and 1. Note that 0 and a number with all 1s are not considered to have 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”.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation of the above 
# approach 
    
# Function to check
def hasEqualBlockFrequency(N):
    
    # Converting integer to its equivalent 
    # binary number
    S = bin(N).replace("0b", "")
    p = set()
    c = 1
  
    for i in range(len(S)-1):
  
        # If adjacent character are same 
        # then increase counter
        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")
  
# Driver code
N = 5
hasEqualBlockFrequency(N)

chevron_right


Output:

Yes

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if a number has
// same counts of 0s and 1s in every
// block.
  
#include <iostream>
using namespace std;
  
// function to convert decimal to binary
bool isEqualBlock(int n)
{
    // Count same bits in last block
    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 is 0 or it has all 1s,
    // then it is not considered to
    // have equal number of 0s and
    // 1s in blocks.
    if (n == 0)
        return false;
  
    // Count same bits in all remaining blocks.
    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;
}
  
// Driver program to test above function
int main()
{
    int n = 51;
    if (isEqualBlock(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if a number has
// same counts of 0s and 1s in every
// block.
import java.io.*;
  
class GFG
{
  
// function to convert decimal to binary
static boolean isEqualBlock(int n)
{
    // Count same bits in last block
    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 is 0 or it has all 1s,
    // then it is not considered to
    // have equal number of 0s and
    // 1s in blocks.
    if (n == 0)
        return false;
  
    // Count same bits in all remaining blocks.
    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;
}
  
// Driver code
public static void main (String[] args)
{
    int n = 51;
    if (isEqualBlock(n))
        System.out.println( "Yes");
    else
        System.out.println("No");
}
}
  
// This code is contributed by inder_mca

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to check if a number has 
# same counts of 0s and 1s in every block. 
  
# Function to convert decimal to binary 
def isEqualBlock(n): 
  
    # Count same bits in last block 
    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 is 0 or it has all 1s, 
    # then it is not considered to 
    # have equal number of 0s and 
    # 1s in blocks. 
    if n == 0:
        return False
  
    # Count same bits in all remaining blocks. 
    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
  
# Driver Code
if __name__ == "__main__"
  
    n = 51
    if isEqualBlock(n): 
        print("Yes")
    else:
        print("No"
      
# This code is contributed by 
# Rituraj Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if a number has 
// same counts of 0s and 1s in every 
// block. 
  
using System;
  
class GFG 
  
    // function to convert decimal to binary 
    static bool isEqualBlock(int n) 
    
        // Count same bits in last block 
        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 is 0 or it has all 1s, 
        // then it is not considered to 
        // have equal number of 0s and 
        // 1s in blocks. 
        if (n == 0) 
            return false
      
        // Count same bits in all remaining blocks. 
        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
    
      
    // Driver code 
    public static void Main () 
    
        int n = 51; 
          
        if (isEqualBlock(n)) 
            Console.WriteLine( "Yes"); 
        else
            Console.WriteLine("No"); 
    
  
// This code is contributed by Ryuga

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check if a number has
// same counts of 0s and 1s in every
// block.
  
// function to convert decimal to binary
function isEqualBlock($n)
{
    // Count same bits in last block
    $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 is 0 or it has all 1s, then 
    // it is not considered to have equal 
    // number of 0s and 1s in blocks.
    if ($n == 0)
        return false;
  
    // Count same bits in all remaining blocks.
    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;
}
  
// Driver Code
$n = 51;
if (isEqualBlock($n))
    echo "Yes";
else
    echo "No";
  
// This code is contributed by
// Shivi_Aggarwal 
?>

chevron_right


Output:

Yes


My Personal Notes arrow_drop_up

Computer Science enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.