We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent aff5013 commit 48c6338Copy full SHA for 48c6338
2024/02 - February/21-02-2024.cpp
@@ -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