Problem solution · C++

Maximum Product of Two Integers with No Common Bits

Maximum Product of Two Integers with No Common Bits: a C++ solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Dynamic programming
Source
Kamyu LeetCode Solutions
Length
72 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For Maximum Product of Two Integers with No Common Bits, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 72 lines of C++ from the credited upstream file maximum-product-of-two-integers-with-no-common-bits.cpp.
  • The implementation visibly relies on sequence storage, cached states.
  • 9 loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeMaximum Product of Two Integers with No Common Bits · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(n + rlogr), r = max(nums)// Space: O(r) // dp, bitmasksclass Solution {public:    long long maxProduct(vector<int>& nums) {        const auto& bit_length = [](int x) {            return (x ? std::__lg(x) : -1) + 1;        };         const auto& l = bit_length(ranges::max(nums));        vector<int> dp(1 << l);        for (const auto& x : nums) {            dp[x] = x;        }        for (int i = 0; i < l; ++i) {            for (int j = 0; j < (1 << l); j += 1 << (i + 1)) {                for (int k = j; k < j + (1 << i); ++k) {                    if (dp[k] > dp[k + (1 << i)]) {                        dp[k + (1 << i)] = dp[k];                    }                }            }        }        int64_t result = 0;        for (const auto& x : nums) {            const auto& v = static_cast<int64_t>(x) * dp[((1 << l) - 1) ^ x];            if (v > result) {                result = v;            }        }        return result;    }}; // Time:  O(n + rlogr), r = max(nums)// Space: O(r)// dp, bitmasksclass Solution2 {public:    long long maxProduct(vector<int>& nums) {        const auto& bit_length = [](int x) {            return (x ? std::__lg(x) : -1) + 1;        };         const auto& l = bit_length(ranges::max(nums));        vector<int> dp(1 << l);        for (const auto& x : nums) {            dp[x] = x;        }        for (int i = 0; i < l; ++i) {            for (int mask = 0; mask < (1 << l); ++mask) {                if (mask & (1 << i)) {                    continue;                }                if (dp[mask] > dp[mask | (1 << i)]) {                    dp[mask | (1 << i)] = dp[mask];                }            }        }        int64_t result = 0;        for (const auto& x : nums) {            const auto& v = static_cast<int64_t>(x) * dp[((1 << l) - 1) ^ x];            if (v > result) {                result = v;            }        }        return result;    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗