Number of Effective Subsequences Problem
Description
LeetCode Problem 3757.
You are given an integer array nums.
The strength of the array is defined as the bitwise OR of all its elements.
A subsequence is considered effective if removing that subsequence strictly decreases the strength of the remaining elements.
Return the number of effective subsequences in nums. Since the answer may be large, return it modulo 10^9 + 7.
The bitwise OR of an empty array is 0.
Example 1:
Input: nums = [1,2,3]
Output: 3
Explanation:
- The Bitwise OR of the array is 1 OR 2 OR 3 = 3.
- Subsequences that are effective are:
- [1, 3]: The remaining element [2] has a Bitwise OR of 2.
- [2, 3]: The remaining element [1] has a Bitwise OR of 1.
- [1, 2, 3]: The remaining elements [] have a Bitwise OR of 0.
- Thus, the total number of effective subsequences is 3.
Example 2:
Input: nums = [7,4,6]
Output: 4
Explanation:
- The Bitwise OR of the array is 7 OR 4 OR 6 = 7.
- Subsequences that are effective are:
- [7]: The remaining elements [4, 6] have a Bitwise OR of 6.
- [7, 4]: The remaining element [6] has a Bitwise OR of 6.
- [7, 6]: The remaining element [4] has a Bitwise OR of 4.
- [7, 4, 6]: The remaining elements [] have a Bitwise OR of 0.
- Thus, the total number of effective subsequences is 4.
Example 3:
Input: nums = [8,8]
Output: 1
Explanation:
- The Bitwise OR of the array is 8 OR 8 = 8.
- Only the subsequence [8, 8] is effective since removing it leaves [] which has a Bitwise OR of 0.
- Thus, the total number of effective subsequences is 1.
Example 4:
Input: nums = [2,2,1]
Output: 5
Explanation:
- The Bitwise OR of the array is 2 OR 2 OR 1 = 3.
- Subsequences that are effective are:
- [1]: The remaining elements [2, 2] have a Bitwise OR of 2.
- [2, 1] (using nums[0], nums[2]): The remaining element [2] has a Bitwise OR of 2.
- [2, 1] (using nums[1], nums[2]): The remaining element [2] has a Bitwise OR of 2.
- [2, 2]: The remaining element [1] has a Bitwise OR of 1.
- [2, 2, 1]: The remaining elements [] have a Bitwise OR of 0.
- Thus, the total number of effective subsequences is 5.
Constraints:
1 <= nums.length <= 10^51 <= nums[i] <= 10^6
Solution
Approach: Counting with Inclusion-Exclusion
Let T be the OR of the whole array. A subsequence is effective when the elements left behind have an OR that is not T. So count the subsets R of elements to keep whose OR is not T, and the answer is 2^n - (number of subsets with OR exactly T).
To count subsets whose OR is a submask of some mask m, let f[m] be how many numbers use only bits of m. Every subset of those numbers works, so there are 2^f[m] of them. f is computed for all masks with a sum-over-subsets pass: for each bit, add f[m without that bit] into f[m]. Values are below 220, so there are 20 bits.
Subsets with OR exactly T come from inclusion-exclusion over the submasks m of T: add 2^f[m] when T \ m has an even number of bits, subtract it when odd. Subtract the result from 2^n (all modulo 109 + 7).
Complexity: O(n + B 2B) time and O(2B) space, where B = 20 is the number of bits.
class Solution:
def countEffective(self, nums: List[int]) -> int:
mod = 10**9 + 7
n = len(nums)
t = 0
for x in nums:
t |= x
bits = [b for b in range(20) if t >> b & 1]
k = len(bits)
full = 1 << k
# f[m] = how many numbers use only the bits in m (after compressing to the bits of t)
f = [0] * full
for x in nums:
m = 0
for i, b in enumerate(bits):
if x >> b & 1:
m |= 1 << i
f[m] += 1
for i in range(k):
h = 1 << i
for start in range(0, full, 2 * h):
lo = f[start:start + h]
hi = f[start + h:start + 2 * h]
f[start + h:start + 2 * h] = [a + b for a, b in zip(hi, lo)]
pow2 = [1] * (n + 1)
for i in range(1, n + 1):
pow2[i] = pow2[i - 1] * 2 % mod
# subsets whose OR is exactly t, by inclusion-exclusion over the bits we leave out
parity = [0]
for _ in range(k):
parity += [1 - v for v in parity]
exact = 0
for m in range(full):
if parity[(full - 1) ^ m]:
exact -= pow2[f[m]]
else:
exact += pow2[f[m]]
# a subsequence is effective exactly when what remains does not have OR t
return (pow2[n] - exact) % mod