|
| 1 | +# [3125. Maximum Number That Makes Result of Bitwise AND Zero 🔒](https://leetcode.com/problems/maximum-number-that-makes-result-of-bitwise-and-zero) |
| 2 | + |
| 3 | +[中文文档](/solution/3100-3199/3125.Maximum%20Number%20That%20Makes%20Result%20of%20Bitwise%20AND%20Zero/README.md) |
| 4 | + |
| 5 | +<!-- tags: --> |
| 6 | + |
| 7 | +## Description |
| 8 | + |
| 9 | +Given an integer <code>n</code>, return the <strong>maximum</strong> integer <code>x</code> such that <code>x <= n</code>, and the bitwise <code>AND</code> of all the numbers in the range <code>[x, n]</code> is 0. |
| 10 | + |
| 11 | +<p> </p> |
| 12 | +<p><strong class="example">Example 1:</strong></p> |
| 13 | + |
| 14 | +<div class="example-block"> |
| 15 | +<p><strong>Input:</strong> <span class="example-io">n = 7</span></p> |
| 16 | + |
| 17 | +<p><strong>Output:</strong> <span class="example-io">3</span></p> |
| 18 | + |
| 19 | +<p><strong>Explanation:</strong></p> |
| 20 | + |
| 21 | +<p>The bitwise <code>AND</code> of <code>[6, 7]</code> is 6.<br /> |
| 22 | +The bitwise <code>AND</code> of <code>[5, 6, 7]</code> is 4.<br /> |
| 23 | +The bitwise <code>AND</code> of <code>[4, 5, 6, 7]</code> is 4.<br /> |
| 24 | +The bitwise <code>AND</code> of <code>[3, 4, 5, 6, 7]</code> is 0.</p> |
| 25 | +</div> |
| 26 | + |
| 27 | +<p><strong class="example">Example 2:</strong></p> |
| 28 | + |
| 29 | +<div class="example-block"> |
| 30 | +<p><strong>Input:</strong> <span class="example-io">n = 9</span></p> |
| 31 | + |
| 32 | +<p><strong>Output:</strong> <span class="example-io">7</span></p> |
| 33 | + |
| 34 | +<p><strong>Explanation:</strong></p> |
| 35 | + |
| 36 | +<p>The bitwise <code>AND</code> of <code>[7, 8, 9]</code> is 0.</p> |
| 37 | +</div> |
| 38 | + |
| 39 | +<p><strong class="example">Example 3:</strong></p> |
| 40 | + |
| 41 | +<div class="example-block"> |
| 42 | +<p><strong>Input:</strong> <span class="example-io">n = 17</span></p> |
| 43 | + |
| 44 | +<p><strong>Output:</strong> <span class="example-io">15</span></p> |
| 45 | + |
| 46 | +<p><strong>Explanation:</strong></p> |
| 47 | + |
| 48 | +<p>The bitwise <code>AND</code> of <code>[15, 16, 17]</code> is 0.</p> |
| 49 | +</div> |
| 50 | + |
| 51 | +<p> </p> |
| 52 | +<p><strong>Constraints:</strong></p> |
| 53 | + |
| 54 | +<ul> |
| 55 | + <li><code>1 <= n <= 10<sup>15</sup></code></li> |
| 56 | +</ul> |
| 57 | + |
| 58 | +## Solutions |
| 59 | + |
| 60 | +### Solution 1: Bit Manipulation |
| 61 | + |
| 62 | +We can find the highest bit of $1$ in the binary representation of $n$. The maximum $x$ must be less than $n$ and this bit is $0$, and all other lower bits are $1$, i.e., $x = 2^{\text{number of the highest bit}} - 1$. This is because $x \text{ and } (x + 1) = 0$ must hold. |
| 63 | + |
| 64 | +The time complexity is $O(\log n)$, and the space complexity is $O(1)$. |
| 65 | + |
| 66 | +<!-- tabs:start --> |
| 67 | + |
| 68 | +```python |
| 69 | +class Solution: |
| 70 | + def maxNumber(self, n: int) -> int: |
| 71 | + return (1 << (n.bit_length() - 1)) - 1 |
| 72 | +``` |
| 73 | + |
| 74 | +```java |
| 75 | +class Solution { |
| 76 | + public long maxNumber(long n) { |
| 77 | + return (1L << (63 - Long.numberOfLeadingZeros(n))) - 1; |
| 78 | + } |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +```cpp |
| 83 | +class Solution { |
| 84 | +public: |
| 85 | + long long maxNumber(long long n) { |
| 86 | + return (1LL << (63 - __builtin_clzll(n))) - 1; |
| 87 | + } |
| 88 | +}; |
| 89 | +``` |
| 90 | +
|
| 91 | +```go |
| 92 | +func maxNumber(n int64) int64 { |
| 93 | + return int64(1<<(bits.Len64(uint64(n))-1)) - 1 |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +<!-- tabs:end --> |
| 98 | + |
| 99 | +<!-- end --> |
0 commit comments