Skip to content

Commit 48c6338

Browse files
committed
21-02-2024
1 parent aff5013 commit 48c6338

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

2024/02 - February/21-02-2024.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
3+
Author : Manas Rawat
4+
Date : 21/02/2024
5+
Problem : Bitwise AND of Numbers Range
6+
Difficulty : Medium
7+
Problem Link : https://leetcode.com/problems/bitwise-and-of-numbers-range/description/
8+
Video Solution : NA
9+
10+
*/
11+
12+
13+
class Solution {
14+
public:
15+
int rangeBitwiseAnd(int left, int right) {
16+
long long cur = 0;
17+
long long ans = 0;
18+
19+
for(int i = 0; i < 31; i++){
20+
if((left >> i) & 1){
21+
int dx = right - left;
22+
int poss = (1 << i) - cur - 1;
23+
24+
if(dx <= poss)
25+
ans |= (1 << i);
26+
27+
cur |= (1 << i);
28+
}
29+
}
30+
31+
return ans;
32+
}
33+
};

0 commit comments

Comments
 (0)