• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
July 04, 2022 |5.8K Views
Length of the Longest Consecutive 1s in Binary Representation
  Share  1 Like
Description
Discussion

Given a number n, find length of the longest consecutive 1s in its binary representation.
One simple way would be to simply loop over the bits, and keep track of the number of consecutive set bits, and the maximum that this value has reached. In this approach, we need to convert it to binary (base-2) representation and then find and print the result.
Examples :

Input : n = 14
Output : 3
The binary representation of 14 is 1110.

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


Length of the Longest Consecutive 1s in Binary Representation : https://www.geeksforgeeks.org/length-longest-consecutive-1s-binary-representation/

Read More