3133. Minimum Array End
Problem
Build a strictly increasing array of n positive integers whose bitwise AND is exactly x. Return the smallest possible value of its last element.
Example: n = 3, x = 4 → 6 ([4,5,6]); n = 2, x = 7 → 15
Constraints: n, x ≤ 10⁸.
Every element must contain all of x's set bits, so elements are x with some of its zero bits filled in. The n-th smallest such number puts the binary digits of n − 1 into x's zero-bit positions (low to high) — "deposit" n − 1 into the free bits.
x = 4 = 0b100 free bits: positions 0, 1, 3, 4, ...
n = 3 -> n-1 = 0b10 -> bit 0 <- 0, bit 1 <- 1 -> 0b110 = 6 (array 4, 5, 6)
Complexity: O(log n + log x).
class Solution:
def minEnd(self, n: int, x: int) -> int:
res, k, bit = x, n - 1, 1
while k:
if not x & bit: # free position: place the next bit of n - 1
if k & 1:
res |= bit
k >>= 1
bit <<= 1
return res
assert Solution().minEnd(3, 4) == 6
assert Solution().minEnd(2, 7) == 15
assert Solution().minEnd(1, 5) == 5
assert Solution().minEnd(5, 1) == 9
def brute(n, x): # n-th smallest number containing all bits of x
v, seen = x, 0
while True:
if v & x == x:
seen += 1
if seen == n:
return v
v += 1
assert all(Solution().minEnd(n, x) == brute(n, x) for n in range(1, 25) for x in range(1, 33))
class Solution {
public long minEnd(int n, int x) {
long res = x, k = n - 1;
for (long bit = 1; k > 0; bit <<= 1) {
if ((x & bit) == 0) {
if ((k & 1) == 1) res |= bit;
k >>= 1;
}
}
return res;
}
}
func minEnd(n int, x int) int64 {
res, k := int64(x), int64(n-1)
for bit := int64(1); k > 0; bit <<= 1 {
if int64(x)&bit == 0 { // free position: place the next bit of n - 1
res |= (k & 1) * bit
k >>= 1
}
}
return res
}