Construct the Minimum Bitwise Array II

You are given an array nums consisting of n prime integers.

You need to construct an array ans of length n, such that, for each index i, the bitwise OR of ans[i] and ans[i] + 1 is equal to nums[i], i.e. ans[i] OR (ans[i] + 1) == nums[i].

Additionally, you must minimize each value of ans[i] in the resulting array.

If it is not possible to find such a value for ans[i] that satisfies the condition, then set ans[i] = -1.

Example 1
Inputnums = [2,3,5,7]
Output[-1,1,4,3]
For each index, the output gives the smallest value satisfying ans[i] OR (ans[i] + 1) == nums[i], or -1 when no such value exists.
Example 2
Inputnums = [11,13,31]
Output[9,12,15]
The smallest values satisfying the condition for 11, 13, and 31 are 9, 12, and 15, respectively.

Constraints

  • 1 <= nums.length <= 100
  • 2 <= nums[i] <= 10^9
  • nums[i] is a prime number.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate