A high-security vault logs its internal arming counter twice during every inspection cycle: once while the counter reads some value k, and again one tick later while it reads k + 1. The vault's aging display can only show the bitwise OR of the two readings it captured, so only that OR value is ever written into the permanent inspection log -- never k or k + 1 by themselves. Auditors have confirmed that every value that has ever appeared in the log happens to be a prime number.
Given n prime log entries, recover, for each entry x, the smallest non-negative integer k for which k OR (k + 1) equals x -- the earliest possible counter reading consistent with that entry. There is exactly one prime that can never be produced by this OR process no matter what k is chosen; for any entry equal to that prime, report -1 instead.
Line 1: an integer n. Line 2: n space-separated integers, the log entries nums[0], ..., nums[n-1]. Each is guaranteed to be a prime number.
Print n integers separated by single spaces: for each i, the minimum non-negative integer k satisfying k OR (k + 1) == nums[i], or -1 if no such k exists.
Example 1
Input
3 2 3 5
Expected
-1 1 4
Explanation
For x = 3, k = 1 gives 1 OR 2 = 3 (binary 01 OR 10 = 11), and no smaller k works, so the answer is 1. For x = 5, k = 4 gives 4 OR 5 = 5 (binary 100 OR 101 = 101), and no smaller k works, so the answer is 4. For x = 2, no non-negative integer k makes k OR (k + 1) equal 2, so the answer is -1. Output: "-1 1 4".
Example 2
Input
2 7 11
Expected
3 9
Explanation
For x = 7, k = 3 gives 3 OR 4 = 7 (binary 011 OR 100 = 111), and no smaller k works. For x = 11, k = 9 gives 9 OR 10 = 11 (binary 1001 OR 1010 = 1011), and no smaller k works. Output: "3 9".
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →