-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeek hates too many 1s(GFG).cpp
More file actions
41 lines (34 loc) · 968 Bytes
/
Geek hates too many 1s(GFG).cpp
File metadata and controls
41 lines (34 loc) · 968 Bytes
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
// class Solution {
// public:
// int noConseBits(int n) {
// // ans will store the required result
// // i is number use to iterate over number and check whether the ith bit is set or not
// // count will store the number of consecutive ones
// int ans=0, i=31, count=0;
// while(i>=0){
// if(count<=1 && (n&(1<<i))){
// ans = (ans | (1<<i));
// count++;
// }else{
// count=0;
// }
// i--;
// }
// return ans;
// }
// };
class Solution {
public:
int noConseBits(int n) {
for(int i = 30; i>=2; i--)
{
int mask1 = 1<<i;
int mask2 = 1<<(i-1);
int mask3 = 1<<(i-2);
if((mask1&n) && (mask2&n) && (mask3&n)){
n = (n^mask3);
}
}
return n;
}
};