Minimum Set Bits in a Valid Integer Problem
Description
LeetCode Problem 4119.
You are given an integer array nums of length n and an integer k.
Let x_1, x_2, ..., x_(n-k+1) denote the bitwise AND of each subarray of length k in nums, in order from left to right.
A positive integer y is called valid if (y & x_i) != 0 for every 1 <= i <= n - k + 1.
Return the minimum number of set bits in any valid integer y. If no valid integer exists, return -1.
Example 1:
Input: nums = [6,7,3], k = 2
Output: 1
Explanation:
- The two subarrays of length k = 2 are [6, 7] and [7, 3].
- Their bitwise ANDs are x_1 = 6 & 7 = 6 and x_2 = 7 & 3 = 3.
- y = 2 is valid, since 2 & 6 = 2 and 2 & 3 = 2, both non-zero.
- y = 2 has 1 set bit ("010"), so the answer is 1.
Example 2:
Input: nums = [3,5,12], k = 1
Output: 2
Explanation:
- Since k = 1, each subarray has one element, so x_1 = 3, x_2 = 5, and x_3 = 12.
- y = 9 is valid, since 9 & 3 = 1, 9 & 5 = 1, and 9 & 12 = 8, all non-zero.
- y = 9 has 2 set bits ("1001"), so the answer is 2.
Example 3:
Input: nums = [2,1], k = 2
Output: -1
Explanation:
- The only subarray of length k = 2 is [2, 1], with bitwise AND x_1 = 2 & 1 = 0.
- Since x_1 = 0, no y is valid. Thus, the answer is -1.
Constraints:
1 <= n == nums.length <= 10^51 <= k <= n0 <= nums[i] <= 10^5
Solution
Approach: Window ANDs and Subset Marking
First compute the AND of every window of length k. Cut nums into blocks of k elements. Let pre[i] be the AND from the start of i's block to i, and suf[i] the AND from i to the end of its block. The window starting at i spans two neighbouring blocks, so its AND is suf[i] & pre[i + k - 1].
All numbers fit in B bits, and a valid y never needs more bits than that. A number y is invalid when some window AND x has y & x == 0, which means x is a subset of the complement of y. So mark bad[x] = true for each window AND, then spread the marks to supersets (for each bit, bad[m] |= bad[m without that bit]). After that, bad[m] tells whether some window AND is a subset of m.
Now y is valid exactly when bad[full ^ y] is false. Scan all y from 1 to 2^B - 1 and keep the smallest popcount. If no y is valid (this happens when some window AND is 0), the answer is -1.
Complexity: O(n + B * 2^B) time and O(n + 2^B) space, where B is the number of bits of max(nums) (at most 17).
class Solution:
def minSetBits(self, nums: List[int], k: int) -> int:
n = len(nums)
# AND of every window of length k: split nums into blocks of k, then
# the window starting at i is (suffix AND inside its block) & (prefix AND inside the next block)
pre = nums[:]
suf = nums[:]
for i in range(1, n):
if i % k:
pre[i] &= pre[i - 1]
for i in range(n - 2, -1, -1):
if (i + 1) % k:
suf[i] &= suf[i + 1]
B = max(1, max(nums).bit_length())
size = 1 << B
# bad[m]: some window AND is a subset of m, so a y whose complement is m misses that window
bad = [False] * size
for i in range(n - k + 1):
bad[suf[i] & pre[i + k - 1]] = True
for b in range(B):
bit = 1 << b
for m in range(size):
if m & bit and bad[m ^ bit]:
bad[m] = True
ans = -1
for y in range(1, size):
if not bad[(size - 1) ^ y]:
c = bin(y).count("1")
if ans < 0 or c < ans:
ans = c
return ans