3315. Construct the Minimum Bitwise Array II
Difficulty: Medium
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] <= 10⁹-
nums[i]is a prime number.
Hint:
- Consider the binary representation of
nums[i]. - Answer is -1 for even
nums[i]. - Try unsetting a single bit from
nums[i].
Solution:
We are given an array nums of prime numbers. For each prime number p = nums[i], we need to find the smallest non-negative integer x such that x OR (x+1) = p. If no such x exists, we set ans[i] = -1.
Approach:
The problem requires constructing an array ans such that for each i, ans[i] OR (ans[i] + 1) == nums[i], with ans[i] minimized. If no solution exists, ans[i] = -1. The solution leverages bitwise properties of the equation.
Key observations:
- For even
nums[i](specifically 2, the only even prime), no solution exists becausex OR (x+1)is always odd for integerx. - For odd
nums[i], we analyze the binary representation to find the minimalxsatisfying the equation.
Steps:
-
For each prime
pinnums:- If
p == 2, setans[i] = -1. - If
pis of the form2^t - 1(all bits are 1 in binary), setans[i] = (p-1)/2. - Otherwise, find the index
mof the least significant0bit inp(0-indexed from right). Then, setans[i] = p - (1 << (m-1)).
- If
Return the constructed array
ans.
Let's implement this solution in PHP: 3315. Construct the Minimum Bitwise Array II
<?php
/**
* @param Integer[] $nums
* @return Integer[]
*/
function minBitwiseArray(array $nums): array
{
...
...
...
/**
* 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:
Why even primes (except 2) are not considered:
All primes except 2 are odd. For even numbers,x OR (x+1)is always odd, so no solution exists for evennums[i]. Since the input consists of primes, the only even prime is 2, which yields no solution.-
Understanding the equation
x OR (x+1) = p:
Letxbe a number. In binary,x+1flips the trailing1s to0and the first0to1. The OR operation results in a numberpwhere:- All bits to the right of the first
0inxbecome1inp. - The first
0inxbecomes1inp. - Higher bits remain unchanged.
- All bits to the right of the first
Thus, p must have a suffix of at least one 1 (i.e., p is odd). For p to be valid, its binary representation must have a contiguous block of trailing 1s, except possibly when p is all 1s.
Case when
pis all1s (2^t - 1):
Here, anyxwitht-1trailing1s and a0at the next bit works. The minimalxis obtained by setting the highest such bit to0, yieldingx = (p-1)/2.General case (odd
pnot all1s):
Find the least significant0bit inp(indexm). This implies that bits0tom-1inpare1. The minimalxis obtained by turning off the bit at positionm-1inp, which isx = p - (1 << (m-1)). This ensuresxhas the required trailing1s and a0at positionm-1, satisfying the equation.Minimization:
The approach maximizes the power of two subtracted fromp(by choosing the largest possiblem), thereby minimizingx.
Complexity
-
Time Complexity: O(n log(max(nums))), where
nis the length ofnums. For each number, we may iterate through its bits (up to 31 for 32-bit integers). - Space Complexity: O(1) extra space (excluding the output 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)