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.
Input: N = 5
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
Equivalent binary number of 51 is 110011.
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”.
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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Check if binary representation of a number is palindrome
- Check if binary representation of a given number and its complement are anagram
- Check if actual binary representation of a number is palindrome
- Count number of trailing zeros in Binary representation of a number using Bitset
- Number of leading zeros in binary representation of a given number
- Binary representation of next number
- Binary representation of a given number
- Largest number with binary representation is m 1's and m-1 0's
- Next greater number than N with exactly one bit different in binary representation of N
- Binary representation of previous number
- Occurrences of a pattern in binary representation of a number
- Prime Number of Set Bits in Binary Representation | Set 1
- Find the n-th number whose binary representation is a palindrome
- Prime Number of Set Bits in Binary Representation | Set 2
- Number of mismatching bits in the binary representation of two integers
- Find consecutive 1s of length >= n in binary representation of a number
- C program to count zeros and ones in binary representation of a number
- Find the number obtained after concatenation of binary representation of M and N
- Sum of decimal equivalent of all possible pairs of Binary representation of a Number
- Maximum number of consecutive 1's in binary representation of all the array elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.