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:
Input: nums = [2,3,5,7]
Output: [-1,1,4,3]
Explanation:
i = 0, as there is no value for ans[0] that satisfies ans[0] OR (ans[0] + 1) = 2, so ans[0] = -1.i = 1, the smallest ans[1] that satisfies ans[1] OR (ans[1] + 1) = 3 is 1, because 1 OR (1 + 1) = 3.i = 2, the smallest ans[2] that satisfies ans[2] OR (ans[2] + 1) = 5 is 4, because 4 OR (4 + 1) = 5.i = 3, the smallest ans[3] that satisfies ans[3] OR (ans[3] + 1) = 7 is 3, because 3 OR (3 + 1) = 7.Example 2:
Input: nums = [11,13,31]
Output: [9,12,15]
Explanation:
i = 0, the smallest ans[0] that satisfies ans[0] OR (ans[0] + 1) = 11 is 9, because 9 OR (9 + 1) = 11.i = 1, the smallest ans[1] that satisfies ans[1] OR (ans[1] + 1) = 13 is 12, because 12 OR (12 + 1) = 13.i = 2, the smallest ans[2] that satisfies ans[2] OR (ans[2] + 1) = 31 is 15, because 15 OR (15 + 1) = 31.Constraints:
1 <= nums.length <= 1002 <= nums[i] <= 109nums[i] is a prime number.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are given a target number and you need to find a smaller starting number which, when combined with the number just one step larger, creates your target using a specific bitwise combination rule. The brute force approach simply tests every possible smaller number one by one to see which one works.
Here's how the algorithm would work step-by-step:
def construct_min_bitwise_array(target_numbers):
answer_list = []
for target_value in target_numbers:
found_solution = False
# Test every possible starting number sequentially from zero up to the target value
for test_number in range(target_value + 1):
if (test_number | (test_number + 1)) == target_value:
answer_list.append(test_number)
found_solution = True
break
# If we exhausted all possibilities without success, record that no valid answer exists
if not found_solution:
answer_list.append(-1)
return answer_list
Instead of testing every single number to find a match, we can use a clever shortcut based on how binary numbers work. By looking closely at the trailing ones in our target number, we can instantly figure out the smallest matching value without any guesswork.
Here's how the algorithm would work step-by-step:
def min_bitwise_array(target_numbers):
final_results = []
for current_target in target_numbers:
# Even numbers cannot be formed because any number plus itself plus one is odd.
if current_target % 2 == 0:
final_results.append(-1)
continue
trailing_ones_mask = (current_target & (current_target + 1))
target_plus_one = current_target + 1
lowest_trailing_bit = target_plus_one & -target_plus_one
# Flipping the bit before the trailing ones block yields the minimum possible value.
candidate_solution = current_target - (lowest_trailing_bit >> 1)
final_results.append(candidate_solution)
# Returning the complete array of minimum building blocks for all queries.
return final_results
| Case | How to Handle |
|---|---|
| Even prime number input such as 2 | Since 2 has no bitwise OR combination where ans OR (ans >> 1) equals 2, the algorithm correctly assigns negative one. |
| Prime numbers of the form 2^k - 1 such as 3, 7, 31 | The algorithm identifies the longest trailing sequence of ones and unsets the second-to-last one in that sequence to yield the minimum possible answer. |
| Prime numbers with alternating bits or single trailing one such as 5 | The algorithm detects the trailing bit pattern and clears the least significant set bit before the trailing zero block to satisfy the OR condition. |
| Maximum possible prime values fitting within 32-bit or 64-bit integer limits | Bitwise operations are performed using standard integer types without overflow by leveraging efficient bit-shifting and masking techniques. |
| Single element array containing a prime number | The iteration correctly processes the single element and returns a one-element array with the corresponding valid answer or negative one. |
| Array containing duplicate prime values | Each element is processed independently in a loop, ensuring identical inputs yield identical correct outputs in the result array. |
| Large scale inputs with maximum allowed array length | The solution operates in O(N) time complexity with O(1) extra space per element, easily scaling within standard execution time limits. |