# Length of the Longest Consecutive 1s in Binary Representation

Given a number **N**, The task is to find the length of the longest consecutive **1s **series in its binary representation.**Examples :**

Input:N = 14Output:3Explanation:The binary representation of 14 is1110.

Input:N = 222Output:4Explanation:The binary representation of 222 is 11011110.

**Naive Approach:** Below is the idea to solve the problem

Traverse the bits of binary representation of

Nand keep a track of the number of consecutive set bits, and the maximum length of consecutive1sfound so far.

**Time Complexity: **O(X), Here X is the length of binary representation of N.**Auxiliary Space: **O(1)

__Find the length of the longest consecutive 1s____ ____series using Bit Magic:__

Below is the idea to solve the problem:

The idea is based on the concept that the

ANDof bit sequence with aleft shifted by 1version of itself effectively removes the trailing1from every sequence of consecutive1s.So the operation

N= (N & (N << 1))reduces length of every sequence of1sby one in binary representation ofN. If we keep doing this operation in a loop, we end up withN= 0. The number of iterations required to reach0is actually length of the longest consecutive sequence of1s.

**Illustration:**

11101111 (x)

& 11011110 (x << 1)

—————————

11001110 (x & (x << 1))

^ ^

| |

Trailing 1 removed

Follow the below steps to implement the above approach:

- Create a variable count initialized with value
**0**. - Run a while loop till
**N**is not**0.**- In each iteration perform the operation
**N****= (N & (N << 1))** - Increment count by one.

- In each iteration perform the operation
- Return count

Below is the Implementation of above approach:

## C++

`// C++ program to find length of the longest` `// consecutive 1s in binary representation of` `// a number.` `#include<bits/stdc++.h>` `using` `namespace` `std;` `int` `maxConsecutiveOnes(` `int` `x)` `{` ` ` `// Initialize result` ` ` `int` `count = 0;` ` ` `// Count the number of iterations to` ` ` `// reach x = 0.` ` ` `while` `(x!=0)` ` ` `{` ` ` `// This operation reduces length` ` ` `// of every sequence of 1s by one.` ` ` `x = (x & (x << 1));` ` ` `count++;` ` ` `}` ` ` `return` `count;` `}` `// Driver code` `int` `main()` `{` ` ` `// Function Call` ` ` `cout << maxConsecutiveOnes(14) << endl;` ` ` `cout << maxConsecutiveOnes(222) << endl;` ` ` `return` `0;` `}` |

## Java

`// Java program to find length of the longest` `// consecutive 1s in binary representation of` `// a number.` `class` `MaxConsecutiveOnes` `{` ` ` `private` `static` `int` `maxConsecutiveOnes(` `int` `x)` ` ` `{` ` ` `// Initialize result` ` ` `int` `count = ` `0` `;` ` ` `// Count the number of iterations to` ` ` `// reach x = 0.` ` ` `while` `(x!=` `0` `)` ` ` `{` ` ` `// This operation reduces length` ` ` `// of every sequence of 1s by one.` ` ` `x = (x & (x << ` `1` `));` ` ` `count++;` ` ` `}` ` ` `return` `count;` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main(String strings[])` ` ` `{` ` ` `System.out.println(maxConsecutiveOnes(` `14` `));` ` ` `System.out.println(maxConsecutiveOnes(` `222` `));` ` ` `}` `}` |

## Python3

`# Python program to find` `# length of the longest` `# consecutive 1s in` `# binary representation of` `# a number.` `def` `maxConsecutiveOnes(x):` ` ` `# Initialize result` ` ` `count ` `=` `0` ` ` ` ` `# Count the number of iterations to` ` ` `# reach x = 0.` ` ` `while` `(x!` `=` `0` `):` ` ` ` ` `# This operation reduces length` ` ` `# of every sequence of 1s by one.` ` ` `x ` `=` `(x & (x << ` `1` `))` ` ` ` ` `count` `=` `count` `+` `1` ` ` ` ` `return` `count` `# Driver code` `print` `(maxConsecutiveOnes(` `14` `))` `print` `(maxConsecutiveOnes(` `222` `))` `# This code is contributed` `# by Anant Agarwal.` |

## C#

`// C# program to find length of the` `// longest consecutive 1s in binary` `// representation of a number.` `using` `System;` `class` `GFG {` ` ` ` ` `// Function to find length of the` ` ` `// longest consecutive 1s in binary` ` ` `// representation of a number` ` ` `private` `static` `int` `maxConsecutiveOnes(` `int` `x)` ` ` `{` ` ` ` ` `// Initialize result` ` ` `int` `count = 0;` ` ` `// Count the number of iterations` ` ` `// to reach x = 0.` ` ` `while` `(x != 0)` ` ` `{` ` ` ` ` `// This operation reduces length` ` ` `// of every sequence of 1s by one.` ` ` `x = (x & (x << 1));` ` ` `count++;` ` ` `}` ` ` `return` `count;` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `Console.WriteLine(maxConsecutiveOnes(14));` ` ` `Console.Write(maxConsecutiveOnes(222));` ` ` `}` `}` `// This code is contributed by Nitin Mittal.` |

## PHP

`<?php` `// PHP program to find length` `// of the longest consecutive` `// 1s in binary representation of` `// a number.` `function` `maxConsecutiveOnes(` `$x` `)` `{` ` ` `// Initialize result` ` ` `$count` `= 0;` ` ` `// Count the number of` ` ` `// iterations to reach x = 0.` ` ` `while` `(` `$x` `!= 0)` ` ` `{` ` ` `// This operation reduces` ` ` `// length of every sequence` ` ` `// of 1s by one.` ` ` `$x` `= (` `$x` `& (` `$x` `<< 1));` ` ` `$count` `++;` ` ` `}` ` ` `return` `$count` `;` `}` `// Driver code` `echo` `maxConsecutiveOnes(14), ` `"\n"` `;` `echo` `maxConsecutiveOnes(222), ` `"\n"` `;` `// This code is contributed by Ajit` `?>` |

## Javascript

`<script>` `// Javascript program to find length` `// of the longest consecutive 1s in` `// binary representation of a number.` `function` `maxConsecutiveOnes(x)` `{` ` ` ` ` `// Initialize result` ` ` `let count = 0;` ` ` `// Count the number of iterations to` ` ` `// reach x = 0.` ` ` `while` `(x != 0)` ` ` `{` ` ` ` ` `// This operation reduces length` ` ` `// of every sequence of 1s by one.` ` ` `x = (x & (x << 1));` ` ` `count++;` ` ` `}` ` ` `return` `count;` `}` `// Driver code` `document.write(maxConsecutiveOnes(14) + ` `"<br/>"` `);` `document.write(maxConsecutiveOnes(222));` `// This code is contributed by code_hunt ` `</script>` |

**Output**

3 4

**Time Complexity: **O(log X), Here X is the length of binary representation of N **Auxiliary Space: **O(1)

This article is contributed by **Pankaj Kumar**. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.