3915. Maximum Product of Two Integers With No Common Bits

Medium
Array
Dynamic Programming
Bit Manipulation

Description

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 <= 105
  • 1 <= nums[i] <= 106

Hints

Hint 1
Think of each number as a mask: treat <code>nums[i]</code> as a bitmask.
Hint 2
Create an array <code>dp</code> of size <code>1 << B</code>, where <code>B</code> is your bit‑width.
Hint 3
Initialize <code>dp[mask]</code> to the maximum <code>nums[i]</code> exactly equal to that <code>mask</code>, or 0 if none.
Hint 4
For each <code>m</code>, propagate to all its super‑masks <code>M</code>: <code>dp[m] = max(dp[m], dp[M])</code>
Hint 5
For a number <code>x</code> with mask <code>mx</code>, compute its "complement mask" as <code>cm = ~mx & ((1 << B)-1)</code>.
Hint 6
The best disjoint partner is then <code>dp[cm]</code>.
Hint 7
Loop over all <code>x</code> in <code>nums</code>, look up <code>dp[cm]</code>, and track the maximum <code>x * partner</code>.

Statistics

Acceptance
12.3%
Submissions
68,398
Accepted
8,388