Ctrl K

Smallest OR With Its Successor

Medium
You get an array of primes primes. For each primes[i], find the smallest non-negative integer a such that a OR (a + 1) == primes[i], where OR is the bitwise OR of a number and the number right after it. If no such a exists, put -1 in that position. Return the array of answers, of the same length and in the same order.
Example 1
primes=[19, 2, 23] → [17, -1, 19]

19 = 10011₂: 17 OR 18 = 10001₂ OR 10010₂ = 10011₂ = 19, and nothing smaller works. For 2 there is no answer: one of two neighbouring numbers is odd, so a OR (a + 1) is always odd, hence -1. For 23 = 10111₂ the answer is 19: 19 OR 20 = 23.

Example 2
primes=[3, 31] → [1, 15]

1 OR 2 = 3, and 15 OR 16 = 11111₂ = 31.

Example 3
primes=[5] → [4]
Constraints
  • 1 ≤ len(primes) ≤ 100
  • 2 ≤ primes[i] ≤ 10⁹
  • Every primes[i] is a prime number
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEvery Topping Combo MediumAll Distinct Card Hands MediumThe Odd One Among Triples MediumTwo Values Without a Pair MediumSpot the Extra Copy Medium
loading Python…
Input
primes=[19, 2, 23]
Expected
[17, -1, 19]

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