HomeLeetcode201. Bitwise AND of Numbers Range - Leetcode Solutions

201. Bitwise AND of Numbers Range – Leetcode Solutions

Description

Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.

Examples:

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

Solution in Python

To solve the problem of finding the bitwise AND of all numbers in the given range [left, right], we need to understand how the bitwise AND operation works across multiple numbers.

Key Insight:

When we perform a bitwise AND operation across multiple consecutive numbers, the result will only retain the common bits that are the same in all those numbers. If any bit position differs among the numbers in the range, that bit will be set to 0 in the result.

Approach:

  1. Shift the Range: The idea is to keep shifting both left and right to the right until they are equal. Every time we shift, we essentially remove the bits that differ between left and right.
  2. Track the Shifts: We need to keep track of how many shifts we perform because we will need to shift the result back to the left by the same amount to get the final answer.
  3. Return the Result: Once left equals right after shifting, the bits that remain in left (or right, since they are now equal) represent the common bits in the original range. Shift these bits back to their original position to obtain the result.
Python
class Solution:
    def rangeBitwiseAnd(self, left: int, right: int) -> int:
        # Initialize a counter for the number of shifts
        shift_count = 0
        
        # Shift left and right to the right until they are equal
        while left < right:
            left >>= 1  # Shift left to the right by 1 bit
            right >>= 1  # Shift right to the right by 1 bit
            shift_count += 1  # Increment the shift counter
        
        # Once left equals right, shift back to the left by shift_count to get the result
        return left << shift_count

Example Walkthrough:

For left = 5 and right = 7:

  • Binary of 5 is 101
  • Binary of 6 is 110
  • Binary of 7 is 111
  1. After 1st shift:
    • left becomes 2 (from 5 -> 101 to 10)
    • right becomes 3 (from 7 -> 111 to 11)
  2. After 2nd shift:
    • left becomes 1 (from 2 -> 10 to 1)
    • right becomes 1 (from 3 -> 11 to 1)

Now left equals right, and the remaining bits in left (which is 1) represent the common prefix. After shifting it back by 2 (the number of shifts), the result is 4 (which is 100 in binary).

This approach efficiently computes the bitwise AND for large ranges without iterating through all the numbers.

Solution in Javascript

JavaScript
/**
 * @param {number} left
 * @param {number} right
 * @return {number}
 */
var rangeBitwiseAnd = function(left, right) {
    // Initialize a counter for the number of shifts
    let shiftCount = 0;
    
    // Shift left and right to the right until they are equal
    while (left < right) {
        left >>= 1;  // Shift left to the right by 1 bit
        right >>= 1; // Shift right to the right by 1 bit
        shiftCount++; // Increment the shift counter
    }
    
    // Once left equals right, shift back to the left by shiftCount to get the result
    return left << shiftCount;
};

Solution in Java

Java
class Solution {
    public int rangeBitwiseAnd(int left, int right) {
        // Initialize a counter for the number of shifts
        int shiftCount = 0;
        
        // Shift left and right to the right until they are equal
        while (left < right) {
            left >>= 1;  // Shift left to the right by 1 bit
            right >>= 1; // Shift right to the right by 1 bit
            shiftCount++; // Increment the shift counter
        }
        
        // Once left equals right, shift back to the left by shiftCount to get the result
        return left << shiftCount;
    }
}

Solution in C#

C#
public class Solution {
    public int RangeBitwiseAnd(int left, int right) {
        // Initialize a counter for the number of right shifts
        int shiftCount = 0;

        // Keep shifting left and right to the right until they are equal
        while (left < right) {
            left >>= 1;  // Right shift the left boundary by 1 bit
            right >>= 1; // Right shift the right boundary by 1 bit
            shiftCount++; // Increment the shift counter
        }

        // Shift left back to the left by shiftCount to get the result
        return left << shiftCount;
    }
}

Subscribe
Notify of

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Popular