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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- In-place replace multiple occurrences of a pattern
- Pattern Occurrences : Stack Implementation Java
- Count of occurrences of a "1(0+)1" pattern in a string
- Binary representation of next greater number with same number of 1's and 0's
- Number of leading zeros in binary representation of a given number
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Count number of trailing zeros in Binary representation of a number using Bitset
- Check if binary representation of a number is palindrome
- Binary representation of next number
- Check if binary representation of a given number and its complement are anagram
- Binary representation of previous number
- Largest number with binary representation is m 1's and m-1 0's
- Check if actual binary representation of a number is palindrome
- Prime Number of Set Bits in Binary Representation | Set 1
- Length of longest consecutive zeroes in the binary representation of a number.
- Next greater number than N with exactly one bit different in binary representation of N
- Number of mismatching bits in the binary representation of two integers
- Find consecutive 1s of length >= n in binary representation of a number
- Minimum sub-array such that number of 1's in concatenation of binary representation of its elements is at least K
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.