Maximum Number That Makes Result of Bitwise AND Zero - Problem
Given an integer n, return the maximum integer x such that x ≤ n, and the bitwise AND of all the numbers in the range [x, n] is 0.
The bitwise AND operation compares each bit position of two numbers and returns 1 only when both bits are 1, otherwise returns 0.
When we AND all numbers in a range, we need to find the largest starting point where this result becomes 0.
Input & Output
Example 1 — Basic Case
$
Input:
n = 5
›
Output:
3
💡 Note:
We need to find the maximum x where AND of range [x,5] equals 0. For x=3, we get 3&4&5 = 0&5 = 0 (since 3&4=0). For x=4, we get 4&5=4≠0. So maximum x is 3.
Example 2 — Single Number
$
Input:
n = 1
›
Output:
0
💡 Note:
For x=0, the range [0,1] computes 0&1 = 0. This gives AND result 0, so x=0 is the maximum answer.
Example 3 — Edge Case Zero
$
Input:
n = 0
›
Output:
0
💡 Note:
For x=0, the range [0,0] contains only 0, and AND of 0 is 0. So x=0 is the answer.
Constraints
- 0 ≤ n ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code