Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using bit manipulation strategy.
Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.
Example 1:
Input: left = 5, right = 7 Output: 4
Example 2:
Input: left = 0, right = 0 Output: 0
Example 3:
Input: left = 1, right = 2147483647 Output: 0
Constraints:
0 <= left <= right <= 231 - 1Problem summary: Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Bit Manipulation
5 7
0 0
1 2147483647
longest-nice-subarray)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #201: Bitwise AND of Numbers Range
class Solution {
public int rangeBitwiseAnd(int left, int right) {
while (left < right) {
right &= (right - 1);
}
return right;
}
}
// Accepted solution for LeetCode #201: Bitwise AND of Numbers Range
func rangeBitwiseAnd(left int, right int) int {
for left < right {
right &= (right - 1)
}
return right
}
# Accepted solution for LeetCode #201: Bitwise AND of Numbers Range
class Solution:
def rangeBitwiseAnd(self, left: int, right: int) -> int:
while left < right:
right &= right - 1
return right
// Accepted solution for LeetCode #201: Bitwise AND of Numbers Range
struct Solution;
impl Solution {
fn range_bitwise_and(mut m: i32, mut n: i32) -> i32 {
let mut shift = 0;
while m != n {
m >>= 1;
n >>= 1;
shift += 1;
}
n << shift
}
}
#[test]
fn test() {
let m = 5;
let n = 7;
let res = 4;
assert_eq!(Solution::range_bitwise_and(m, n), res);
let m = 0;
let n = 1;
let res = 0;
assert_eq!(Solution::range_bitwise_and(m, n), res);
}
// Accepted solution for LeetCode #201: Bitwise AND of Numbers Range
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #201: Bitwise AND of Numbers Range
// class Solution {
// public int rangeBitwiseAnd(int left, int right) {
// while (left < right) {
// right &= (right - 1);
// }
// return right;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.