top of page
Search

Maximum number of consecutive 1's in Binary representation of a number

  • Ben
  • Feb 13, 2018
  • 1 min read

#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <queue> #include <stack> #include <string> #include <bitset> #include <cstdio> #include <limits> #include <vector> #include <climits> #include <cstring> #include <cstdlib> #include <fstream> #include <numeric> #include <sstream> #include <iostream> #include <algorithm> #include <unordered_map>

using namespace std;

int main(){ // Get an input base 10 integer int n; cin >> n; int i=0; int k=0; long int a[10000]; long int b[10000]; long int c[10000]; long int count; // Find the binary of the number while(n>0) { a[i] = n%2; n=n/2; i++; } // Display the binary and store it in array b which is commented here for(int j=i; j>=0; j--) { b[k] = a[j]; // cout<< a[j] ; k++; } //cout<< endl; // Display array b which is commented for (int j=0; j<=i; j++) { // cout<< b[j]; } // Find the maximum number of consecutive 1's in a given no int max = 0; for(long int j=0; j<=i; j++) { if (b[j]==0) { count= 0; } else { count++; if(count > max) { max = count; } } } cout<< max; return 0; }

 
 
 

Recent Posts

See All
Swap two words in a byte

#include <iostream> using namespace std; int main() { unsigned int data=0x1234; cout<<"data before swapping :"<<hex<<data<<endl; ...

 
 
 
Fibonacci series

#include <iostream> using namespace std; int main() { // Getting the input of fibonacci series long int n; cout<<"Enter the size of...

 
 
 

Comments


bottom of page