Skip to content

Commit 53320a1

Browse files
authored
Merge pull request #1099 from Asymtode712/LCSolutions
Added LeetCode Solutions 201-202
2 parents dba1a77 + e51e20f commit 53320a1

File tree

2 files changed

+494
-0
lines changed

2 files changed

+494
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
id: bitwise-and-of-numbers-range
3+
title: Bitwise AND of Numbers Range
4+
sidebar_label: 201 Bitwise AND of Numbers Range
5+
tags:
6+
- Bit Manipulation
7+
- C++
8+
- Python
9+
- Java
10+
description: "This document provides a solution where we return the bitwise AND of all numbers in a given range."
11+
---
12+
13+
## Problem
14+
15+
Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.
16+
17+
### Examples
18+
19+
**Example 1:**
20+
21+
```
22+
Input: left = 5, right = 7
23+
24+
Output: 4
25+
26+
```
27+
**Example 2:**
28+
```
29+
Input: left = 0, right = 0
30+
31+
Output: 0
32+
33+
```
34+
**Example 3:**
35+
```
36+
Input: left = 1, right = 2147483647
37+
38+
Output: 0
39+
40+
```
41+
42+
### Constraints
43+
44+
- `0 <= left <= right <= 2^31 - 1`
45+
46+
---
47+
48+
### Intuition
49+
The problem is asking to find the bitwise AND of all numbers in a range. The first thing that comes to mind is that the bitwise AND of a number with a larger number will always be less than or equal to the number. This is because the AND operation can only unset bits, not set them. Therefore, the result will be the common prefix of the binary representations of the left and right numbers, with the rest of the bits set to zero.
50+
51+
## Approach
52+
53+
**Simple Bit Manipulation:**
54+
55+
The approach is to right shift both the left and right numbers until they become equal. This is equivalent to finding the common prefix of their binary representations. The number of times we need to right shift is the number of trailing zeros in the result. Then we left shift the common prefix by this count to get the result.
56+
57+
- Let's understand this with a simple example. Suppose we have `left =` 5 and `right = 7`. If we write these numbers in binary (the language computers understand), we get `101` for `5` and `111` for `7`.
58+
59+
- Now, we need to find the bitwise AND of all numbers from `5` to `7`. In binary, that's like saying `101 AND 110 AND 111`. The result of this operation is `100`.
60+
61+
- Our approach is to keep shifting both `5` and `7` to the right until they become the same number. This is like finding the common part of their binary representations. In our case, we shift `5` and `7` to the right once, and we get `2` and `3` respectively. They're not the same, so we shift again. Now we get `1` and `1`. They're the same, so we stop. We've shifted twice.
62+
63+
- The number of times we shift is the number of zeros at the end of our result. In our case, the result `100` has two zeros at the end, which is the same as the number of times we shifted.
64+
65+
- Finally, we shift our common number (`1`) to the left the same number of times we shifted right before. In our case, we shift `1` to the left twice and get `100`, which is our result.
66+
67+
68+
#### Code in Java
69+
70+
```java
71+
class Solution {
72+
public int rangeBitwiseAnd(int left, int right) {
73+
int cnt = 0;
74+
while (left != right) {
75+
left >>= 1;
76+
right >>= 1;
77+
cnt++;
78+
}
79+
return (left << cnt);
80+
}
81+
}
82+
83+
public static void main(String[] args) {
84+
Solution solution = new Solution();
85+
int left = 5;
86+
int right = 7;
87+
System.out.println("Bitwise AND of range [" + left + ", " + right + "] is " + solution.rangeBitwiseAnd(left, right));
88+
}
89+
}
90+
91+
```
92+
93+
#### Code in C++
94+
95+
```cpp
96+
#include <iostream>
97+
using namespace std;
98+
99+
class Solution {
100+
public:
101+
int rangeBitwiseAnd(int left, int right) {
102+
int count = 0;
103+
104+
while (left != right) {
105+
left >>= 1;
106+
right >>= 1;
107+
count++;
108+
}
109+
110+
return (left << count);
111+
}
112+
};
113+
114+
int main() {
115+
Solution solution;
116+
int left = 5;
117+
int right = 7;
118+
cout << "Bitwise AND of range [" << left << ", " << right << "] is " << solution.rangeBitwiseAnd(left, right) << endl;
119+
return 0;
120+
}
121+
```
122+
123+
#### Code in Python
124+
125+
```py
126+
class Solution:
127+
def rangeBitwiseAnd(self, left: int, right: int) -> int:
128+
cnt = 0
129+
while left != right:
130+
left >>= 1
131+
right >>= 1
132+
cnt += 1
133+
return left << cnt
134+
135+
if __name__ == "__main__":
136+
solution = Solution()
137+
left = 5
138+
right = 7
139+
print(f"Bitwise AND of range [{left}, {right}] is {solution.rangeBitwiseAnd(left, right)}")
140+
```
141+
142+
### Complexity Analysis
143+
144+
#### Time Complexity: $O(log n)$
145+
146+
> **Reason**: This is because in each iteration of the while loop, the numbers `left` and `right` are divided by `2` (right-shifted by 1 bit), which is equivalent to halving the numbers. Thus, the number of iterations required to make `left` equal to `right` is proportional to the number of bits in the binary representation of n, which is
147+
$O(log n)$.
148+
149+
#### Space Complexity: $O(1)$
150+
151+
> **Reason**: The space complexity is $O(1)$, because we only use a constant amount of extra space regardless of the input size.
152+
153+
# References
154+
155+
- **LeetCode Problem:** [Number of Digit One](https://leetcode.com/problems/bitwise-and-of-numbers-range/description/)
156+
- **Solution Link:** [Number of Digit One Solution on LeetCode](https://leetcode.com/problems/bitwise-and-of-numbers-range/solutions/)
157+
- **Authors LeetCode Profile:** [Vivek Vardhan](https://leetcode.com/u/sidkul712/)

0 commit comments

Comments
 (0)