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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array nums.
Your task is to find two distinct indices i and j such that the product nums[i] * nums[j] is maximized, and the binary representations of nums[i] and nums[j] do not share any common set bits.
Return the maximum possible product of such a pair. If no such pair exists, return 0.
Example 1:
Input: nums = [1,2,3,4,5,6,7]
Output: 12
Explanation:
The best pair is 3 (011) and 4 (100). They share no set bits and 3 * 4 = 12.
Example 2:
Input: nums = [5,6,4]
Output: 0
Explanation:
Every pair of numbers has at least one common set bit. Hence, the answer is 0.
Example 3:
Input: nums = [64,8,32]
Output: 2048
Explanation:
No pair of numbers share a common bit, so the answer is the product of the two maximum elements, 64 and 32 (64 * 32 = 2048).
Constraints:
2 <= nums.length <= 1051 <= nums[i] <= 106Problem summary: You are given an integer array nums. Your task is to find two distinct indices i and j such that the product nums[i] * nums[j] is maximized, and the binary representations of nums[i] and nums[j] do not share any common set bits. Return the maximum possible product of such a pair. If no such pair exists, return 0.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Bit Manipulation
[1,2,3,4,5,6,7]
[5,6,4]
[64,8,32]
partition-to-k-equal-sum-subsets)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3670: Maximum Product of Two Integers With No Common Bits
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3670: Maximum Product of Two Integers With No Common Bits
// package main
//
// import (
// "math/bits"
// "slices"
// )
//
// // https://space.bilibili.com/206214
// func maxProduct1(nums []int) int64 {
// w := bits.Len(uint(slices.Max(nums)))
// u := 1 << w
// f := make([]int, u)
// for _, x := range nums {
// f[x] = x
// }
//
// for s := range f {
// for t, lb := s, 0; t > 0; t ^= lb {
// lb = t & -t
// f[s] = max(f[s], f[s^lb])
// }
// }
//
// ans := 0
// for _, x := range nums {
// ans = max(ans, x*f[u-1^x])
// }
// return int64(ans)
// }
//
// func maxProduct2(nums []int) int64 {
// w := bits.Len(uint(slices.Max(nums)))
// u := 1 << w
// f := make([]int, u)
// for _, x := range nums {
// f[x] = x
// }
//
// for i := range w {
// for s := 0; s < u; s++ {
// s |= 1 << i // 快速跳到第 i 位是 1 的 j
// f[s] = max(f[s], f[s^1<<i])
// }
// }
//
// ans := 0
// for _, x := range nums {
// ans = max(ans, x*f[u-1^x])
// }
// return int64(ans)
// }
//
// // 基于方法一
// func maxProduct(nums []int) int64 {
// w := bits.Len(uint(slices.Max(nums)))
// u := 1 << w
//
// n := len(nums)
// if n*n <= u*w {
// // 暴力
// ans := 0
// for i, x := range nums {
// for _, y := range nums[:i] {
// if x&y == 0 {
// ans = max(ans, x*y)
// }
// }
// }
// return int64(ans)
// }
//
// f := make([]int, u)
// for _, x := range nums {
// f[x] = x
// }
//
// for s := range f {
// for t, lb := s, 0; t > 0; t ^= lb {
// lb = t & -t
// f[s] = max(f[s], f[s^lb])
// }
// }
//
// ans := 0
// for _, x := range nums {
// ans = max(ans, x*f[u-1^x])
// }
// return int64(ans)
// }
// Accepted solution for LeetCode #3670: Maximum Product of Two Integers With No Common Bits
package main
import (
"math/bits"
"slices"
)
// https://space.bilibili.com/206214
func maxProduct1(nums []int) int64 {
w := bits.Len(uint(slices.Max(nums)))
u := 1 << w
f := make([]int, u)
for _, x := range nums {
f[x] = x
}
for s := range f {
for t, lb := s, 0; t > 0; t ^= lb {
lb = t & -t
f[s] = max(f[s], f[s^lb])
}
}
ans := 0
for _, x := range nums {
ans = max(ans, x*f[u-1^x])
}
return int64(ans)
}
func maxProduct2(nums []int) int64 {
w := bits.Len(uint(slices.Max(nums)))
u := 1 << w
f := make([]int, u)
for _, x := range nums {
f[x] = x
}
for i := range w {
for s := 0; s < u; s++ {
s |= 1 << i // 快速跳到第 i 位是 1 的 j
f[s] = max(f[s], f[s^1<<i])
}
}
ans := 0
for _, x := range nums {
ans = max(ans, x*f[u-1^x])
}
return int64(ans)
}
// 基于方法一
func maxProduct(nums []int) int64 {
w := bits.Len(uint(slices.Max(nums)))
u := 1 << w
n := len(nums)
if n*n <= u*w {
// 暴力
ans := 0
for i, x := range nums {
for _, y := range nums[:i] {
if x&y == 0 {
ans = max(ans, x*y)
}
}
}
return int64(ans)
}
f := make([]int, u)
for _, x := range nums {
f[x] = x
}
for s := range f {
for t, lb := s, 0; t > 0; t ^= lb {
lb = t & -t
f[s] = max(f[s], f[s^lb])
}
}
ans := 0
for _, x := range nums {
ans = max(ans, x*f[u-1^x])
}
return int64(ans)
}
# Accepted solution for LeetCode #3670: Maximum Product of Two Integers With No Common Bits
#
# @lc app=leetcode id=3670 lang=python3
#
# [3670] Maximum Product of Two Integers With No Common Bits
#
# @lc code=start
class Solution:
def maxProduct(self, nums: List[int]) -> int:
# The maximum value in nums is 10^6, which is less than 2^20 (1,048,576).
# We need 20 bits to represent all numbers.
NUM_BITS = 20
MAX_MASK = 1 << NUM_BITS
# dp[mask] will store the maximum number in 'nums' that is a submask of 'mask'.
# Initialize with 0.
dp = [0] * MAX_MASK
# Fill dp with the numbers present in the input.
# If a number x is present, it is a submask of itself.
for num in nums:
dp[num] = max(dp[num], num)
# Propagate the maximum values using SOS DP (Sum Over Subsets).
# For each bit position 'i', and each 'mask', if the 'i-th' bit is set in 'mask',
# then any submask of 'mask ^ (1 << i)' is also a submask of 'mask'.
# We want dp[mask] to be the max value among all submasks.
for i in range(NUM_BITS):
bit = 1 << i
for mask in range(MAX_MASK):
if mask & bit:
# If the i-th bit is set, we can either keep it or turn it off.
# Turning it off gives us the submask 'mask ^ bit'.
# We take the max of the current value and the value from the submask.
if dp[mask ^ bit] > dp[mask]:
dp[mask] = dp[mask ^ bit]
max_prod = 0
full_mask = MAX_MASK - 1
# Iterate through each number to find its best disjoint pair.
for num in nums:
# The complement contains all bits NOT set in num.
# Any number that is disjoint from 'num' must be a submask of 'complement'.
complement = full_mask ^ num
# dp[complement] gives the largest number from the array that fits into the complement mask.
partner = dp[complement]
# If partner is 0, it means no valid disjoint number was found (or only 0, which gives product 0).
current_prod = num * partner
if current_prod > max_prod:
max_prod = current_prod
return max_prod
# @lc code=end
// Accepted solution for LeetCode #3670: Maximum Product of Two Integers With No Common Bits
// 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 #3670: Maximum Product of Two Integers With No Common Bits
// package main
//
// import (
// "math/bits"
// "slices"
// )
//
// // https://space.bilibili.com/206214
// func maxProduct1(nums []int) int64 {
// w := bits.Len(uint(slices.Max(nums)))
// u := 1 << w
// f := make([]int, u)
// for _, x := range nums {
// f[x] = x
// }
//
// for s := range f {
// for t, lb := s, 0; t > 0; t ^= lb {
// lb = t & -t
// f[s] = max(f[s], f[s^lb])
// }
// }
//
// ans := 0
// for _, x := range nums {
// ans = max(ans, x*f[u-1^x])
// }
// return int64(ans)
// }
//
// func maxProduct2(nums []int) int64 {
// w := bits.Len(uint(slices.Max(nums)))
// u := 1 << w
// f := make([]int, u)
// for _, x := range nums {
// f[x] = x
// }
//
// for i := range w {
// for s := 0; s < u; s++ {
// s |= 1 << i // 快速跳到第 i 位是 1 的 j
// f[s] = max(f[s], f[s^1<<i])
// }
// }
//
// ans := 0
// for _, x := range nums {
// ans = max(ans, x*f[u-1^x])
// }
// return int64(ans)
// }
//
// // 基于方法一
// func maxProduct(nums []int) int64 {
// w := bits.Len(uint(slices.Max(nums)))
// u := 1 << w
//
// n := len(nums)
// if n*n <= u*w {
// // 暴力
// ans := 0
// for i, x := range nums {
// for _, y := range nums[:i] {
// if x&y == 0 {
// ans = max(ans, x*y)
// }
// }
// }
// return int64(ans)
// }
//
// f := make([]int, u)
// for _, x := range nums {
// f[x] = x
// }
//
// for s := range f {
// for t, lb := s, 0; t > 0; t ^= lb {
// lb = t & -t
// f[s] = max(f[s], f[s^lb])
// }
// }
//
// ans := 0
// for _, x := range nums {
// ans = max(ans, x*f[u-1^x])
// }
// return int64(ans)
// }
// Accepted solution for LeetCode #3670: Maximum Product of Two Integers With No Common Bits
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3670: Maximum Product of Two Integers With No Common Bits
// package main
//
// import (
// "math/bits"
// "slices"
// )
//
// // https://space.bilibili.com/206214
// func maxProduct1(nums []int) int64 {
// w := bits.Len(uint(slices.Max(nums)))
// u := 1 << w
// f := make([]int, u)
// for _, x := range nums {
// f[x] = x
// }
//
// for s := range f {
// for t, lb := s, 0; t > 0; t ^= lb {
// lb = t & -t
// f[s] = max(f[s], f[s^lb])
// }
// }
//
// ans := 0
// for _, x := range nums {
// ans = max(ans, x*f[u-1^x])
// }
// return int64(ans)
// }
//
// func maxProduct2(nums []int) int64 {
// w := bits.Len(uint(slices.Max(nums)))
// u := 1 << w
// f := make([]int, u)
// for _, x := range nums {
// f[x] = x
// }
//
// for i := range w {
// for s := 0; s < u; s++ {
// s |= 1 << i // 快速跳到第 i 位是 1 的 j
// f[s] = max(f[s], f[s^1<<i])
// }
// }
//
// ans := 0
// for _, x := range nums {
// ans = max(ans, x*f[u-1^x])
// }
// return int64(ans)
// }
//
// // 基于方法一
// func maxProduct(nums []int) int64 {
// w := bits.Len(uint(slices.Max(nums)))
// u := 1 << w
//
// n := len(nums)
// if n*n <= u*w {
// // 暴力
// ans := 0
// for i, x := range nums {
// for _, y := range nums[:i] {
// if x&y == 0 {
// ans = max(ans, x*y)
// }
// }
// }
// return int64(ans)
// }
//
// f := make([]int, u)
// for _, x := range nums {
// f[x] = x
// }
//
// for s := range f {
// for t, lb := s, 0; t > 0; t ^= lb {
// lb = t & -t
// f[s] = max(f[s], f[s^lb])
// }
// }
//
// ans := 0
// for _, x := range nums {
// ans = max(ans, x*f[u-1^x])
// }
// return int64(ans)
// }
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: 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.