Ctrl K

AND Down to Zero

Medium
You are given an integer top. Find the largest integer x ≤ top such that the bitwise AND of every number from x to top inclusive equals zero.
Example 1
top=12 → 7

The AND of 8 through 12 is 8, but adding 7 (0111 in binary) brings it down to 0.

Example 2
top=1 → 0

The range [1, 1] gives 1, while [0, 1] already gives 0.

Example 3
top=32 → 31
Constraints
  • 1 ≤ top ≤ 10¹⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumNo Twin Letters Side by Side MediumRound Prices to a Total MediumOne Word Outranks the Other MediumDistinct Letter Counts Medium
loading Python…
Input
top=12
Expected
7

Run checks the first 3 tests in your browser. Submit checks all 74 tests on the server, hidden ones included.