Ctrl K

Smallest OR Neighbor

Easy
You get a list of primes primes. For each p, find the smallest non-negative integer a such that the bitwise OR of a and a + 1 equals p: a | (a + 1) == p If no such a exists, use -1. Return the answers in the same order as the numbers in primes.
Example 1
primes=[5, 19] → [4, 17]

4 OR 5 = 5, and nothing smaller works. For 19 (10011) the answer is 17 (10001): 17 OR 18 = 19.

Example 2
primes=[2, 3] → [-1, 1]

a OR (a + 1) is always odd, so 2 has no answer: -1. For 3 it is 1, since 1 OR 2 = 3.

Example 3
primes=[31, 23] → [15, 19]
Constraints
  • 1 ≤ len(primes) ≤ 100
  • 2 ≤ primes[i] ≤ 1000
  • Every primes[i] is prime
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Unpaired Value EasyThe Lost Ticket EasyDoubled Tag, Lost Tag EasyMirror and Negate a Bitmap EasyMultiple of Five at Every Step Easy
loading Python…
Input
primes=[5, 19]
Expected
[4, 17]

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