3314. Construct the Minimum Bitwise Array I
Difficulty: Easy
Topics: Array, Bit Manipulation, Biweekly Contest 141
You are given an array nums consisting of n prime1 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:
- For
i = 0, as there is no value forans[0]that satisfiesans[0] OR (ans[0] + 1) = 2, soans[0] = -1. - For
i = 1, the smallestans[1]that satisfiesans[1] OR (ans[1] + 1) = 3is1, because1 OR (1 + 1) = 3. - For
i = 2, the smallestans[2]that satisfiesans[2] OR (ans[2] + 1) = 5is4, because4 OR (4 + 1) = 5. - For
i = 3, the smallestans[3]that satisfiesans[3] OR (ans[3] + 1) = 7is3, because3 OR (3 + 1) = 7.
- For
Example 2:
- Input: nums = [11,13,31]
- Output: [9,12,15]
-
Explanation:
- For
i = 0, the smallestans[0]that satisfiesans[0] OR (ans[0] + 1) = 11is9, because9 OR (9 + 1) = 11. - For
i = 1, the smallestans[1]that satisfiesans[1] OR (ans[1] + 1) = 13is12, because12 OR (12 + 1) = 13. - For
i = 2, the smallestans[2]that satisfiesans[2] OR (ans[2] + 1) = 31is15, because15 OR (15 + 1) = 31.
- For
Constraints:
1 <= nums.length <= 1002 <= nums[i] <= 1000-
nums[i]is a prime number.
Hint:
- The constraints are small, allowing you to iterate over all potential values for
ans[i]directly.
Solution:
We need to construct an array ans such that for each i, ans[i] OR (ans[i] + 1) == nums[i], with ans[i] minimized. If no such value exists, set ans[i] = -1.
Approach:
-
Iterate through each prime number in the input array
nums. -
Handle the special case where the number is 2 (the only even prime). Since
x OR (x+1)is always odd, there is no solution for 2. Set the answer to -1. -
For odd primes, iterate through all possible values of
xfrom 0 ton-1(sincex+1must be ≤n). -
Check the condition
x OR (x+1) == nfor eachx. The first (smallest)xthat satisfies the condition is chosen. -
If no such
xis found (which should not happen for odd primes, asx = n-1always works), set the answer to -1.
Let's implement this solution in PHP: 3314. Construct the Minimum Bitwise Array I
<?php
/**
* @param Integer[] $nums
* @return Integer[]
*/
function minBitwiseArray($nums) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo minBitwiseArray([2,3,5,7]) . "\n"; // Output: [-1,1,4,3]
echo minBitwiseArray([11,13,31]) . "\n"; // Output: [9,12,15]
?>
Explanation:
- The bitwise OR of two consecutive integers
xandx+1always results in an odd number (except whenxis negative, but we consider non-negativex). Therefore, even primes (only 2) have no solution. - For odd primes, a solution always exists: setting
x = n-1(which is even) gives(n-1) OR n = nbecause:- The least significant bit of
n-1is 0 and ofnis 1. - All higher bits are identical in
n-1andn.
- The least significant bit of
- However, a smaller
xmay exist (e.g., forn=3,x=1is smaller thanx=2). Therefore, we search fromx=0upward to find the minimum validx. - The constraints allow brute force: at most 1000 iterations per prime, with up to 100 primes, totalling 100,000 operations.
Complexity
-
Time Complexity: O(n * m), where
nis the length ofnumsandmis the maximum value innums(up to 1000). In practice, this is efficient given the constraints. - Space Complexity: O(n) for storing the result array.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
-
*Prime : * A prime number is a natural number greater than 1 with only two factors, 1 and itself. ↩
Top comments (0)