Given two integers x and n, the task is to search for the first consecutive stream of 1s (in the x’s 32-bit binary representation) which is greater than or equal to n in length and return its position. If no such string exists then return -1.
Input: x = 35, n = 2
Binary representation of 35 is 00000000000000000000000000100011 and two consecutive 1’s are present at position 31.
Input: x = 32, n = 3
32 = 00000000000000000000000000100000 in binary and it does not have a sub-string of 3 consecutive 1’s.
Approach: Use Bitwise operation to calculate the no. of leading zeros in the number and then use it to find the position from where we need to start searching for consecutive 1’s. Skip the search for leading zeros.
Below is the implementation of the above approach:
- Length of longest consecutive zeroes in the binary representation of a number.
- Length of the Longest Consecutive 1s in Binary Representation
- 1 to n bit numbers with no consecutive 1s in binary representation.
- 1 to n bit numbers with no consecutive 1s in binary representation
- Find the n-th number whose binary representation is a palindrome
- Length of longest consecutive ones by at most one swap in a Binary String
- Find value of k-th bit in binary representation
- Binary representation of a given number
- Count number of trailing zeros in Binary representation of a number using Bitset
- Check if the binary representation of a number has equal number of 0s and 1s in blocks
- Largest number with binary representation is m 1's and m-1 0's
- Binary representation of previous number
- Sum of digits with even number of 1's in their binary representation
- Next greater number than N with exactly one bit different in binary representation of N
- Find longest sequence of 1's in binary representation with one flip
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.
Improved By : Sach_Code