Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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 109 + 7.
The bitwise OR of an empty array is 0.
Example 1:
Input: nums = [1,2,3]
Output: 3
Explanation:
1 OR 2 OR 3 = 3.[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.Example 2:
Input: nums = [7,4,6]
Output: 4
Explanation:
7 OR 4 OR 6 = 7.[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.Example 3:
Input: nums = [8,8]
Output: 1
Explanation:
8 OR 8 = 8.[8, 8] is effective since removing it leaves [] which has a Bitwise OR of 0.Example 4:
Input: nums = [2,2,1]
Output: 5
Explanation:
2 OR 2 OR 1 = 3.[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.Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 106Problem summary: 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 109 + 7. The bitwise OR of an empty array is 0.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Dynamic Programming · Bit Manipulation
[1,2,3]
[7,4,6]
[8,8]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3757: Number of Effective Subsequences
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3757: Number of Effective Subsequences
// package main
//
// import "math/bits"
//
// // https://space.bilibili.com/206214
// const mod = 1_000_000_007
// const maxN = 100_001
//
// var pow2 = [maxN]int{1}
//
// func init() {
// // 预处理 2 的幂
// for i := 1; i < maxN; i++ {
// pow2[i] = pow2[i-1] * 2 % mod
// }
// }
//
// func countEffective(nums []int) int {
// or := 0
// for _, x := range nums {
// or |= x
// }
//
// w := bits.Len(uint(or))
// f := make([]int, 1<<w)
// for _, x := range nums {
// f[x]++
// }
// for i := range w {
// if or>>i&1 == 0 { // 优化:or 中是 0 的比特位无需计算
// continue
// }
// for s := 0; s < 1<<w; s++ {
// s |= 1 << i
// f[s] += f[s^1<<i]
// }
// }
// // 计算完毕后,f[s] 表示 nums 中的是 s 的子集的元素个数
//
// ans := pow2[len(nums)] // 所有子序列的个数
// // 枚举 or 的所有子集(包括空集)
// for sub, ok := or, true; ok; ok = sub != or {
// sign := 1 - bits.OnesCount(uint(or^sub))%2*2
// ans -= sign * pow2[f[sub]]
// sub = (sub - 1) & or
// }
// return (ans%mod + mod) % mod // 保证结果非负
// }
// Accepted solution for LeetCode #3757: Number of Effective Subsequences
package main
import "math/bits"
// https://space.bilibili.com/206214
const mod = 1_000_000_007
const maxN = 100_001
var pow2 = [maxN]int{1}
func init() {
// 预处理 2 的幂
for i := 1; i < maxN; i++ {
pow2[i] = pow2[i-1] * 2 % mod
}
}
func countEffective(nums []int) int {
or := 0
for _, x := range nums {
or |= x
}
w := bits.Len(uint(or))
f := make([]int, 1<<w)
for _, x := range nums {
f[x]++
}
for i := range w {
if or>>i&1 == 0 { // 优化:or 中是 0 的比特位无需计算
continue
}
for s := 0; s < 1<<w; s++ {
s |= 1 << i
f[s] += f[s^1<<i]
}
}
// 计算完毕后,f[s] 表示 nums 中的是 s 的子集的元素个数
ans := pow2[len(nums)] // 所有子序列的个数
// 枚举 or 的所有子集(包括空集)
for sub, ok := or, true; ok; ok = sub != or {
sign := 1 - bits.OnesCount(uint(or^sub))%2*2
ans -= sign * pow2[f[sub]]
sub = (sub - 1) & or
}
return (ans%mod + mod) % mod // 保证结果非负
}
# Accepted solution for LeetCode #3757: Number of Effective Subsequences
#
# @lc app=leetcode id=3757 lang=python3
#
# [3757] Number of Effective Subsequences
#
# @lc code=start
class Solution:
def countEffective(self, nums: List[int]) -> int:
MOD = 10**9 + 7
if not nums:
return 0
T = 0
for x in nums:
T |= x
if T == 0:
return 0
# The maximum possible value is bounded by the smallest power of 2 > T
limit_bits = T.bit_length()
LIMIT = 1 << limit_bits
dp = [0] * LIMIT
for x in nums:
dp[x] += 1
# SOS DP (Sum Over Subsets)
# dp[mask] will store the count of numbers in nums that are submasks of 'mask'
for i in range(limit_bits):
if (T >> i) & 1:
bit = 1 << i
# Iterate through pairs (k, k+bit) where k has the i-th bit unset
for base in range(0, LIMIT, 2 * bit):
for k in range(base, base + bit):
dp[bit + k] += dp[k]
n = len(nums)
pow2 = [1] * (n + 1)
for i in range(1, n + 1):
pow2[i] = (pow2[i-1] * 2) % MOD
count_eq_T = 0
t_pop = T.bit_count()
# Principle of Inclusion-Exclusion to find number of subsequences with OR sum exactly T
# We iterate over all submasks of T
sub = T
while True:
# dp[sub] is the count of numbers in nums that are submasks of 'sub'
# The number of subsequences using only these numbers is 2^dp[sub]
term = pow2[dp[sub]]
# PIE sign depends on the parity of the difference in set bits
if (t_pop - sub.bit_count()) & 1:
count_eq_T = (count_eq_T - term + MOD) % MOD
else:
count_eq_T = (count_eq_T + term) % MOD
if sub == 0:
break
# Efficiently iterate to the next submask
sub = (sub - 1) & T
# The answer is Total Subsequences - Subsequences with OR sum equal to T
# Total subsequences is 2^n
return (pow2[n] - count_eq_T + MOD) % MOD
# @lc code=end
// Accepted solution for LeetCode #3757: Number of Effective Subsequences
// Rust example auto-generated from go reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (go):
// // Accepted solution for LeetCode #3757: Number of Effective Subsequences
// package main
//
// import "math/bits"
//
// // https://space.bilibili.com/206214
// const mod = 1_000_000_007
// const maxN = 100_001
//
// var pow2 = [maxN]int{1}
//
// func init() {
// // 预处理 2 的幂
// for i := 1; i < maxN; i++ {
// pow2[i] = pow2[i-1] * 2 % mod
// }
// }
//
// func countEffective(nums []int) int {
// or := 0
// for _, x := range nums {
// or |= x
// }
//
// w := bits.Len(uint(or))
// f := make([]int, 1<<w)
// for _, x := range nums {
// f[x]++
// }
// for i := range w {
// if or>>i&1 == 0 { // 优化:or 中是 0 的比特位无需计算
// continue
// }
// for s := 0; s < 1<<w; s++ {
// s |= 1 << i
// f[s] += f[s^1<<i]
// }
// }
// // 计算完毕后,f[s] 表示 nums 中的是 s 的子集的元素个数
//
// ans := pow2[len(nums)] // 所有子序列的个数
// // 枚举 or 的所有子集(包括空集)
// for sub, ok := or, true; ok; ok = sub != or {
// sign := 1 - bits.OnesCount(uint(or^sub))%2*2
// ans -= sign * pow2[f[sub]]
// sub = (sub - 1) & or
// }
// return (ans%mod + mod) % mod // 保证结果非负
// }
// Accepted solution for LeetCode #3757: Number of Effective Subsequences
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3757: Number of Effective Subsequences
// package main
//
// import "math/bits"
//
// // https://space.bilibili.com/206214
// const mod = 1_000_000_007
// const maxN = 100_001
//
// var pow2 = [maxN]int{1}
//
// func init() {
// // 预处理 2 的幂
// for i := 1; i < maxN; i++ {
// pow2[i] = pow2[i-1] * 2 % mod
// }
// }
//
// func countEffective(nums []int) int {
// or := 0
// for _, x := range nums {
// or |= x
// }
//
// w := bits.Len(uint(or))
// f := make([]int, 1<<w)
// for _, x := range nums {
// f[x]++
// }
// for i := range w {
// if or>>i&1 == 0 { // 优化:or 中是 0 的比特位无需计算
// continue
// }
// for s := 0; s < 1<<w; s++ {
// s |= 1 << i
// f[s] += f[s^1<<i]
// }
// }
// // 计算完毕后,f[s] 表示 nums 中的是 s 的子集的元素个数
//
// ans := pow2[len(nums)] // 所有子序列的个数
// // 枚举 or 的所有子集(包括空集)
// for sub, ok := or, true; ok; ok = sub != or {
// sign := 1 - bits.OnesCount(uint(or^sub))%2*2
// ans -= sign * pow2[f[sub]]
// sub = (sub - 1) & or
// }
// return (ans%mod + mod) % mod // 保证结果非负
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.