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.
- Check if binary representation of a number is palindrome
- Check if actual binary representation of a number is palindrome
- Check if binary representation of a given number and its complement are anagram
- 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 a given number
- Binary representation of next number
- Next greater number than N with exactly one bit different in binary representation of N
- Binary representation of previous number
- Largest number with binary representation is m 1's and m-1 0's
- Prime Number of Set Bits in Binary Representation | Set 1
- Find the n-th number whose binary representation is a palindrome
- Occurrences of a pattern in binary representation of a number
- Prime Number of Set Bits in Binary Representation | Set 2
- Find consecutive 1s of length >= n in binary representation of a number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.