We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<bits/stdc++.h>usingnamespacestd;stringltrim(conststring&);stringrtrim(conststring&);intmain(){intbin[32];intconcount[10];intn;intj=0;intcount=0;cin>>n;intx=0;while(n>0){inti=n%2;n=n/2;bin[j]=i;j++;}for(inti=j-1;i>=0;i--){if(bin[i]==1){count+=1;}elseif(bin[i]==0&&count>0){concount[x]=count;x++;count=0;}}// If there were trailing 1sif(count>0){concount[x]=count;// Store the last count of consecutive 1sx++;}intmaxvalue=concount[0];// Output the counts of consecutive 1sfor(inti=0;i<x;i++){// Use x to determine how many counts to print if(concount[i]>maxvalue){maxvalue=concount[i];// Update maxValue if a larger value is found}}cout<<maxvalue;cout<<endl;return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 10: Binary Numbers
You are viewing a single comment's thread. Return to all comments →