3133. Minimum Array End
Difficulty: Medium
Topics: Bit Manipulation
You are given two integers n and x. You have to construct an array of positive integers nums of size n where for every 0 <= i < n - 1, nums[i + 1] is greater than nums[i], and the result of the bitwise AND operation between all elements of nums is x.
Return the minimum possible value of nums[n - 1].
Example 1:
- Input: n = 3, x = 4
- Output: 6
-
Explanation:
numscan be[4,5,6]and its last element is 6.
Example 2:
- Input: n = 2, x = 7
- Output: 15
-
Explanation:
numscan be[7,15]and its last element is 15.
Example 3:
- Input: cost = [[2, 5, 1], [3, 4, 7], [8, 1, 2], [6, 2, 4], [3, 8, 8]]
- Output: 10
Constraints:
1 <= n, x <= 108
Hint:
- Each element of the array should be obtained by “merging”
xandvwherev = 0, 1, 2, …(n - 1). - To merge
xwith another numberv, keep the set bits ofxuntouched, for all the other bits, fill the set bits ofvfrom right to left in order one by one. - So the final answer is the “merge” of
xandn - 1.
Solution:
We need to construct an array nums of positive integers of size n, where each successive element is greater than the previous. The bitwise AND of all elements in nums should yield x. We are asked to find the minimum possible value of nums[n-1].
Here’s the breakdown:
Bit Manipulation Insight: We can observe that
nums[i]should be built by mergingxwith integers0, 1, ..., n-1. This will help ensure the bitwiseANDresult yieldsxsince we start with a base ofx.Building the Array Elements: Each element can be thought of as
xmerged with some integer, and we aim to keepx's bits intact. We fill in additional bits from the integer to get increasing numbers while maintaining theANDoutcome asx.Merging Strategy: To find the minimum
nums[n-1], we only need to mergexwithn-1. Merging in this context means that if any bit inxis1, it remains1. We use bits fromn-1to add any required additional bits without altering the bits set inx.
Let's implement this solution in PHP: 3133. Minimum Array End
<?php
/**
* @param Integer $n
* @param Integer $x
* @return Integer
*/
function minEnd($n, $x) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example 1
echo minimumArrayEnd(3, 4) . "\n"; // Output: 6
// Example 2
echo minimumArrayEnd(2, 7) . "\n"; // Output: 15
?>
Explanation:
-
Bit Checking and Setting:
- We check each bit of
ans(starting fromx), and if a bit is0inans, we look to the corresponding bit ink(which isn-1). - If that bit in
kis1, we set the bit inansto1. This process ensures the minimum increment in value while preserving bits set inx.
- We check each bit of
-
Loop Constraints:
- We iterate through each bit position up to a calculated maximum (
kMaxBit), ensuring that we cover the bits necessary from bothxandn.
- We iterate through each bit position up to a calculated maximum (
-
Result:
- The final value of
ansis the minimum possible value fornums[n-1]that satisfies the conditions.
- The final value of
Complexity:
- The algorithm operates in constant time since the number of bits in any integer in this range is bounded by 32, making this approach efficient even for large values of
nandx.
This solution yields the desired minimum nums[n-1] while maintaining the required properties.
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:
Top comments (0)