Skip to content

Commit f10e2be

Browse files
committed
leetcode
1 parent c2a7d93 commit f10e2be

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
3+
4+
-* 201 Bitwise AND of Numbers Range *-
5+
6+
7+
Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.
8+
9+
10+
11+
Example 1:
12+
13+
Input: left = 5, right = 7
14+
Output: 4
15+
Example 2:
16+
17+
Input: left = 0, right = 0
18+
Output: 0
19+
Example 3:
20+
21+
Input: left = 1, right = 2147483647
22+
Output: 0
23+
24+
25+
Constraints:
26+
27+
0 <= left <= right <= 231 - 1
28+
29+
*/
30+
31+
class Solution {
32+
int rangeBitwiseAnd(int left, int right) {
33+
int shiftCount = 0;
34+
while (left < right) {
35+
left >>= 1;
36+
right >>= 1;
37+
shiftCount += 1;
38+
}
39+
return left << shiftCount;
40+
}
41+
int rangeBitwiseAnd2(int left, int right) {
42+
while (right > left) {
43+
right &= (right - 1);
44+
}
45+
return right;
46+
}
47+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
func rangeBitwiseAnd(left int, right int) int {
4+
var shift_count int = 0
5+
for left < right {
6+
left >>= 1
7+
right >>= 1
8+
shift_count += 1
9+
}
10+
return left << shift_count
11+
12+
for left < right {
13+
right &= (right - 1)
14+
15+
}
16+
return right
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 🔥 Bitwise AND of Numbers Range 🔥 || Simple Fast and Easy || with Explanation
2+
3+
### Intuition and Approach
4+
5+
This code aims to find the bitwise AND of all numbers within the given range [left, right], inclusive. The approach used in this code is to iteratively reduce the `right` number by unsetting its least significant bit until `left` becomes equal to or greater than `right`. Since the range is contiguous, unsetting the least significant bit eventually leads to the common prefix of `left` and `right`, which represents the bitwise AND of all numbers in the range.
6+
7+
### Time Complexity
8+
9+
The time complexity of this approach depends on the number of bits in the input numbers. In the worst case, the loop iterates until `left` becomes equal to or greater than `right`. Each iteration of the loop involves unsetting the least significant bit of `right` using the operation `right &= (right - 1)`, which takes O(1) time. Therefore, the overall time complexity is O(log n), where n is the maximum of `left` and `right`.
10+
11+
### Space Complexity
12+
13+
The space complexity of this code is O(1), as it only uses a constant amount of extra space for storing temporary variables and does not depend on the input size.
14+
15+
This code offers a more efficient approach compared to directly iterating through the range of numbers, making it suitable for finding the bitwise AND of large ranges. However, both approaches aim to achieve the same result.
16+
17+
## Code
18+
19+
```go
20+
func rangeBitwiseAnd(left int, right int) int {
21+
for left < right {
22+
right &= (right - 1)
23+
}
24+
return right
25+
}
26+
27+
// Another Implementation
28+
func rangeBitwiseAnd(left int, right int) int {
29+
var shift_count int = 0
30+
for left < right {
31+
left >>= 1
32+
right >>= 1
33+
shift_count += 1
34+
}
35+
return left << shift_count
36+
}
37+
```
38+
39+
## [GitHub](https://github.com/ayoubzulfiqar/leetcode)

0 commit comments

Comments
 (0)