15% off all plans & roadmaps Use code
See plansProblem editorial
Given two non-negative integers left and right, consider every integer in the inclusive range from left through right. The bitwise AND of two integers keeps a bit set only when that bit is set in both integers; applying it across a collection means every bit must be set in every value.
Bitwise AND of Numbers Range is a Bit Manipulation Patterns problem, in the Bitwise Operations - Power of Two/Four Check subpattern. The pattern covers xOR tricks, counting bits, power-of-two checks, and Hamming weight
The reference solution runs in O(log right), at most 31 iterations time and O(1) space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Bitwise AND of Numbers Range is a Bit Manipulation Patterns problem, in the Bitwise Operations - Power of Two/Four Check subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.