# Find consecutive 1s of length >= n in binary representation of a number

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.

**Examples:**

Input:x = 35, n = 2

Output:31

Binary representation of 35 is 00000000000000000000000000100011 and two consecutive 1’s are present at position 31.

Input:x = 32, n = 3

Output:-1

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:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the ` `// number of leading zeros ` `int` `countLeadingZeros(` `int` `x) ` `{ ` ` ` `unsigned y; ` ` ` `int` `n; ` ` ` `n = 32; ` ` ` `y = x >> 16; ` ` ` `if` `(y != 0) { ` ` ` `n = n - 16; ` ` ` `x = y; ` ` ` `} ` ` ` `y = x >> 8; ` ` ` `if` `(y != 0) { ` ` ` `n = n - 8; ` ` ` `x = y; ` ` ` `} ` ` ` `y = x >> 4; ` ` ` `if` `(y != 0) { ` ` ` `n = n - 4; ` ` ` `x = y; ` ` ` `} ` ` ` `y = x >> 2; ` ` ` `if` `(y != 0) { ` ` ` `n = n - 2; ` ` ` `x = y; ` ` ` `} ` ` ` `y = x >> 1; ` ` ` `if` `(y != 0) ` ` ` `return` `n - 2; ` ` ` `return` `n - x; ` `} ` ` ` `// Function to find the string ` `// of n consecutive 1's ` `int` `FindStringof1s(unsigned x, ` `int` `n) ` `{ ` ` ` `int` `k, p; ` ` ` ` ` `// Initialize position to return. ` ` ` `p = 0; ` ` ` `while` `(x != 0) { ` ` ` ` ` `// Skip leading 0's ` ` ` `k = countLeadingZeros(x); ` ` ` `x = x << k; ` ` ` ` ` `// Set position after leading 0's ` ` ` `p = p + k; ` ` ` ` ` `// Count first group of 1's. ` ` ` `k = countLeadingZeros(~x); ` ` ` ` ` `// If length of consecutive 1's ` ` ` `// is greater than or equal to n ` ` ` `if` `(k >= n) ` ` ` `return` `p + 1; ` ` ` ` ` `// Not enough 1's ` ` ` `// skip over to next group ` ` ` `x = x << k; ` ` ` ` ` `// Update the position ` ` ` `p = p + k; ` ` ` `} ` ` ` ` ` `// if no string is found ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `x = 35; ` ` ` `int` `n = 2; ` ` ` `cout << FindStringof1s(x, n); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` `// Function to count the ` `// number of leading zeros ` `static` `int` `countLeadingZeros(` `int` `x) ` `{ ` ` ` `int` `y; ` ` ` `int` `n; ` ` ` `n = ` `32` `; ` ` ` `y = x >> ` `16` `; ` ` ` `if` `(y != ` `0` `) { ` ` ` `n = n - ` `16` `; ` ` ` `x = y; ` ` ` `} ` ` ` `y = x >> ` `8` `; ` ` ` `if` `(y != ` `0` `) { ` ` ` `n = n - ` `8` `; ` ` ` `x = y; ` ` ` `} ` ` ` `y = x >> ` `4` `; ` ` ` `if` `(y != ` `0` `) { ` ` ` `n = n - ` `4` `; ` ` ` `x = y; ` ` ` `} ` ` ` `y = x >> ` `2` `; ` ` ` `if` `(y != ` `0` `) { ` ` ` `n = n - ` `2` `; ` ` ` `x = y; ` ` ` `} ` ` ` `y = x >> ` `1` `; ` ` ` `if` `(y != ` `0` `) ` ` ` `return` `n - ` `2` `; ` ` ` `return` `n - x; ` `} ` ` ` `// Function to find the string ` `// of n consecutive 1's ` `static` `int` `FindStringof1s(` `int` `x, ` `int` `n) ` `{ ` ` ` `int` `k, p; ` ` ` ` ` `// Initialize position to return. ` ` ` `p = ` `0` `; ` ` ` `while` `(x != ` `0` `) { ` ` ` ` ` `// Skip leading 0's ` ` ` `k = countLeadingZeros(x); ` ` ` `x = x << k; ` ` ` ` ` `// Set position after leading 0's ` ` ` `p = p + k; ` ` ` ` ` `// Count first group of 1's. ` ` ` `k = countLeadingZeros(~x); ` ` ` ` ` `// If length of consecutive 1's ` ` ` `// is greater than or equal to n ` ` ` `if` `(k >= n) ` ` ` `return` `p + ` `1` `; ` ` ` ` ` `// Not enough 1's ` ` ` `// skip over to next group ` ` ` `x = x << k; ` ` ` ` ` `// Update the position ` ` ` `p = p + k; ` ` ` `} ` ` ` ` ` `// if no string is found ` ` ` `return` `-` `1` `; ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `int` `x = ` `35` `; ` ` ` `int` `n = ` `2` `; ` ` ` `System.out.println(FindStringof1s(x, n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` ` ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` `// Function to count the ` `// number of leading zeros ` `static` `int` `countLeadingZeros(` `int` `x) ` `{ ` ` ` `int` `y; ` ` ` `int` `n; ` ` ` `n = 32; ` ` ` `y = x >> 16; ` ` ` `if` `(y != 0) { ` ` ` `n = n - 16; ` ` ` `x = y; ` ` ` `} ` ` ` `y = x >> 8; ` ` ` `if` `(y != 0) { ` ` ` `n = n - 8; ` ` ` `x = y; ` ` ` `} ` ` ` `y = x >> 4; ` ` ` `if` `(y != 0) { ` ` ` `n = n - 4; ` ` ` `x = y; ` ` ` `} ` ` ` `y = x >> 2; ` ` ` `if` `(y != 0) { ` ` ` `n = n - 2; ` ` ` `x = y; ` ` ` `} ` ` ` `y = x >> 1; ` ` ` `if` `(y != 0) ` ` ` `return` `n - 2; ` ` ` `return` `n - x; ` `} ` ` ` `// Function to find the string ` `// of n consecutive 1's ` `static` `int` `FindStringof1s(` `int` `x, ` `int` `n) ` `{ ` ` ` `int` `k, p; ` ` ` ` ` `// Initialize position to return. ` ` ` `p = 0; ` ` ` `while` `(x != 0) { ` ` ` ` ` `// Skip leading 0's ` ` ` `k = countLeadingZeros(x); ` ` ` `x = x << k; ` ` ` ` ` `// Set position after leading 0's ` ` ` `p = p + k; ` ` ` ` ` `// Count first group of 1's. ` ` ` `k = countLeadingZeros(~x); ` ` ` ` ` `// If length of consecutive 1's ` ` ` `// is greater than or equal to n ` ` ` `if` `(k >= n) ` ` ` `return` `p + 1; ` ` ` ` ` `// Not enough 1's ` ` ` `// skip over to next group ` ` ` `x = x << k; ` ` ` ` ` `// Update the position ` ` ` `p = p + k; ` ` ` `} ` ` ` ` ` `// if no string is found ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` ` ` ` ` `static` `public` `void` `Main (){ ` ` ` `int` `x = 35; ` ` ` `int` `n = 2; ` ` ` `Console.WriteLine (FindStringof1s(x, n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

31

## Recommended Posts:

- Length of longest consecutive zeroes in the binary representation of a number.
- Length of the Longest Consecutive 1s in Binary Representation
- Maximum number of consecutive 1's in binary representation of all the array elements
- Find the n-th number whose binary representation is a palindrome
- 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 number obtained after concatenation of binary representation of M and N
- 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
- Binary representation of previous number
- Next greater number than N with exactly one bit different in binary representation of N
- Largest number with binary representation is m 1's and m-1 0's
- Sum of digits with even number of 1's in their binary representation
- Find longest sequence of 1's in binary representation with one flip
- Number of leading zeros in binary representation of a given number
- Prime Number of Set Bits in Binary Representation | Set 1
- Occurrences of a pattern 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 contribute@geeksforgeeks.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.