-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsecutive_1s_in_binary_represntation.cpp
More file actions
94 lines (73 loc) · 1.5 KB
/
consecutive_1s_in_binary_represntation.cpp
File metadata and controls
94 lines (73 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <bits/stdc++.h>
using namespace std;
void solve1(int n){
int count;
if(n==0){ count=0; }
else{ count = 1; }
int max_count = INT_MIN;
int last_bit = (n>>1)&1;
int cur_bit;
n = n>>1;
while(n!=0){
cur_bit = (n>>1)&1;
n = n >> 1;
cout<<last_bit<<" "<<cur_bit<<endl;
if(last_bit==1 && cur_bit==1){
count++;
}
else{
max_count = max(max_count, count);
if((n>>1)&1){ count = 1;}
else{ count = 0; }
}
last_bit = cur_bit;
}
cout<<max_count<<endl;
}
// correct and accepted solution
void solve2(int n){
int count=0;
int max_count = INT_MIN;
while(n!=0){
// cout<< n<<" " << (n & 1) <<" \n";
if(n & 1){
count++;
}
else{
max_count = max(max_count, count);
count = 0;
}
n = n >> 1;
}
max_count = max(max_count, count);
cout<<max_count<<endl;
}
int main()
{
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// solve1(n);
solve2(n);
return 0;
}
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int rem=0,s=0,t=0;
while(n>0){
rem=n%2;
n=n/2;
if(rem==1)
{ s++;
if(s>=t)
t=s;
}
else{
s=0;
}
}
System.out.println(t);
}
}