Given a string pat and an integer N, the task is to find the number of occurrences of the pattern pat in binary representation of N.
Input: N = 2, pat = “101”
Pattern “101” doesn’t occur in the binary representation of 2 (10).
Input: N = 10, pat = “101”
Binary representation of 10 is 1010 and the given pattern occurs only once.
Naive Approach: Convert the number into its binary string representation and then use a pattern matching algorithm to check the number of times the pattern has occurred in the binary representation.
- Convert the binary pattern into it’s decimal representation.
- Take an integer all_ones, whose binary representation consists of all set bits (equal to the number of bits in the pattern).
- Performing N & all_ones now will leave only the last k bits unchanged (others will be 0) where k is the number of bits in the pattern.
- Now if N = pattern, it means that N contained the pattern in the end in its binary representation. So update count = count + 1.
- Right shift N by 1 and repeat the previous two steps until N ≥ pattern & N > 0.
- Print the count in the end.
Below is the implementation of the above approach:
# Python 3 program to find the number of times
# pattern p occurred in binary representation
# on n.
# Function to return the count of occurrence
# of pat in binary representation of n
def countPattern(n, pat):
# To store decimal value of the pattern
pattern_int = 0
power_two = 1
# To store a number that has all ones in
# its binary representation and length
# of ones equal to length of the pattern
all_ones = 0
# Find values of pattern_int and all_ones
i = len(pat) – 1
while(i >= 0):
current_bit = ord(pat[i]) – ord(‘0’)
pattern_int += (power_two * current_bit)
all_ones = all_ones + power_two
power_two = power_two * 2
i -= 1
count = 0
while (n != 0 and n >= pattern_int):
# If the pattern occurs in the last
# digits of n
if ((n & all_ones) == pattern_int):
count += 1
# Right shift n by 1 bit
n = n >> 1
# Driver code
if __name__ == ‘__main__’:
n = 500
pat = “10”
# This code is contributed by
- Binary representation of a given number
- Binary representation of next number
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Binary representation of previous 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
- Number of leading zeros in binary representation of a given number
- Prime Number of Set Bits in Binary Representation | Set 1
- Find the n-th number whose binary representation is a palindrome
- Check if binary representation of a number is palindrome
- Number of mismatching bits in the binary representation of two integers
- Check if actual binary representation of a number is palindrome
- Check if binary representation of a given number and its complement are anagram
- Find consecutive 1s of length >= n in binary representation of a number
- Binary representation of next greater number with same number of 1's and 0's