Longest Consecutive 1's | Practice | GeeksforGeeks

Given a number N. Find the length of the longest consecutive 1s in its binary representation.
Example 1:
Input: N = 14
Output: 3
Explanation: 
Binary representation of 14 is 
1110, in which 111 is the longest 
consecutive set bits of length is 3.
Example 2:
Input: N = 222
Output: 4
Explanation: 
Binary representation of 222 is 
11011110, in which 1111 is the 
longest consecutive set bits of length 4. 


+1
ayushshukla3466in 9 hours
class Solution{   public:   int maxConsecutiveOnes(int N)   {       // code here       int ans = 0,count = 0;       while(N > 0)       {           while(((N & 1) == 1) && (N > 0))           {               count++;               N = N >> 1;           }           if((N & 1) == 0)           {               ans = max(ans,count);               count = 0;               N = N >> 1;           }       }       return ans;   }};
0
vishnu12656 days ago
public static int maxConsecutiveOnes(int N) {
        int count = 0;
        int maxCount = 0;
        while(N>0){
            if(N%2==1){
                count++;
            }
            else{
                maxCount = Math.max(maxCount,count);
                count = 0;
            }
            N = N/2;
        }
        maxCount = Math.max(maxCount,count);
        return maxCount;
        
    }
0
sachinupreti1901 week ago
Easiest one ever seen
 
class Solution{   public:   int maxConsecutiveOnes(int N)   {       // code here       //int p=log2(N)+1;       bitset<32>ok(N);       string s=ok.to_string();       int i,c=0,maxi=-3222;       for(i=0;i<s.size();i++)       {           if(s[i]=='1')           c++;           else           {               maxi=max(maxi,c);               c=0;           }       }       maxi=max(maxi,c);       return maxi;                }};
0
mankesh0162 weeks ago
int ans=0;       while(N){           N&=(N<<1);           ans++;       }       return ans;
0
amansidd178613 weeks ago
int maxConsecutiveOnes(int N)   {       // code here       int mx = 0;       int cnt = 0;       int flag = 0;       while(N){           int rem = N%2;           if(rem==1){               cnt++;               mx = max(mx,cnt);           }           else cnt = 0;           N/=2;       }       return mx;   }
0
gauty verma3 weeks ago
private static int count1sCon(int n) { // TODO Auto-generated method stub int count = 0; int maxCount = 0; boolean flag = true; for(int i = 0;i< 32;i++) {  if((n&(1<<i))!=0) {   flag = true;   count++;   if(count>maxCount) {    maxCount = count;   }  }else {   flag = false;   count = 0;  } } return maxCount;}
0
adityagagtiwari4 weeks ago
easy logn O(1) solution  
public static int maxConsecutiveOnes(int n) {              // Your code here       int maxcount = Integer.MIN_VALUE;       int count = 0;      while(n!=0)      {          if(n%2==1)          {              count++;            // System.out.println(count);                        }          else          {              if(maxcount<count)              {                maxcount  = count;              }            //  System.out.println(maxcount);              //reinitializing count for calculating next series of 1s              count = 0;          }           //.out.println(n);          n=n/2;      }      if(count>maxcount)      {          maxcount =  count;      }      return maxcount;   }
0
ayushahuja2001 month ago
class Solution{   public static int maxConsecutiveOnes(int N) {       int max = Integer.MIN_VALUE;       String ans = Integer.toBinaryString(N);       // System.out.println(ans);       int count = 0;       for(int i = 0;i<ans.length();i++){           if(ans.charAt(i) == '1'){               count ++;           }else{               if(count>max){                   max = Math.max(count,max);                   count = 0;               }else{                   count = 0;               }           }       }       return Math.max(max,count);   }}
0
arjunnigam17131 month ago
def maxConsecutiveOnes(self, n):
        ##Your code here
        global_max = 0
        current_max = 0
        while n>0:
            if n&1!=0:
                current_max +=1
            else:
                global_max = max(global_max, current_max)
                current_max = 0
                
            n=n>>1
        return max(global_max, current_max)


0
vermaashitiit
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.