2Brute2Best
πŸ”₯0

Bitwise AND of Numbers Range

MediumΒ·LeetCode #201 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given two integers, left and right, with left ≀ right. Take every whole number from left to right, both ends included. Return the bitwise AND of all of them together.

Input

Two integers left and right, with left ≀ right.

Output

The AND of every integer from left to right, both included.

Example 1
Input: left = 5, right = 7
Output: 4

5 = 101, 6 = 110, 7 = 111. Their AND is 100 = 4. The number 6 clears the lowest bit that 5 and 7 share.

Example 2
Input: left = 0, right = 0
Output: 0

The only number is 0, so the AND is 0.

Example 3
Input: left = 1, right = 2147483647
Output: 0

The range contains 1 (01) and 2 (10). They share no set bit, so every bit of the AND ends up 0.

Constraints
  • β€’ 0 ≀ left ≀ right ≀ 2^31 βˆ’ 1
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

β€œbitwise AND of all numbers from left to right”
β†’ A range of consecutive integers, not an array. Checking every number is the brute force.
β€œleft and right up to 2^31 βˆ’ 1”
β†’ The range can hold about 2 billion numbers, so the answer must take about 31 steps, not one step per number.
β€œbitwise AND”
β†’ A bit survives only if every number in the range has it set, so the bits the two ends share decide the answer.
03

Approach Complexity Ladder

Brute Force β€” AND every number in the rangeO(N)O(1)

Loop from left to right and AND each number into a running value

Optimal β€” keep the shared top bits, drop the restO(log V)O(1)

Shift both ends right until equal, then shift the common part back

Brute Force Intuition

Start with a running value that has every bit set. Loop from left to right and AND each number into it. When the loop ends, the running value is the answer.

Recognition rule for this sub-pattern:

Look for the common prefix of bits, or use prefix XOR like prefix sums.
Remember this Β· Bit Manipulation
Trigger
"Every element appears twice except…", power of two, counting set bits, subsets as bitmasks, O(1) space tricks.
Think
What happens to this problem if I look at each bit separately?
Key question
Which bit operation (XOR, AND, OR, shift) cancels or isolates what I need?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Bit Manipulation translate into production architectures and low-level memory mechanics.