Talentd
FleetCode

3125. Maximum Number That Makes Result of Bitwise AND Zero

Medium70.0% acceptance1 companyPremium elsewhere, free here
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.

Example 1:

Input: n = 7

Output: 3

Explanation:

The bitwise AND of [6, 7] is 6.
The bitwise AND of [5, 6, 7] is 4.
The bitwise AND of [4, 5, 6, 7] is 4.
The bitwise AND of [3, 4, 5, 6, 7] is 0.

Example 2:

Input: n = 9

Output: 7

Explanation:

The bitwise AND of [7, 8, 9] is 0.

Example 3:

Input: n = 17

Output: 15

Explanation:

The bitwise AND of [15, 16, 17] is 0.

Constraints:

  • 1 <= n <= 1015

Asked at 1 company

Hints open one at a time

Hint 1
Examine the set bits of n.
Hint 2
When performing bitwise AND operations sequentially down from n, the last set bit to turn to 0 identifies the highest set bit.
Hint 3
If the index of the highest set bit is x, the answer is 2x - 1.
6:06
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
7
Input
9
Input
17
Your code is saved on this device.Solution