Skip to main content
Back to problems
#3125
Medium Algorithms

Maximum number that makes result of bitwise and zero

String Greedy Sorting
70.0% acceptance
Mar 31, 2026
12
2
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.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn max_number(n: i64) -> i64 {
    let k = 63 - (n as u64).leading_zeros();
    (1i64 << k) - 1
  }
}