Maximum Product of Subsequences With an Alternating Sum Equal to K Problem
Description
LeetCode Problem 3509.
You are given an integer array nums and two integers, k and limit. Your task is to find a non-empty subsequence of nums that:
- Has an alternating sum equal to
k. - Maximizes the product of all its numbers without the product exceeding
limit.
Return the product of the numbers in such a subsequence. If no subsequence satisfies the requirements, return -1.
The alternating sum of a 0-indexed array is defined as the sum of the elements at even indices minus the sum of the elements at odd indices.
Example 1:
Input: nums = [1,2,3], k = 2, limit = 10
Output: 6
Explanation:
The subsequences with an alternating sum of 2 are:
- [1, 2, 3]
- Alternating Sum: 1 - 2 + 3 = 2
- Product: 1 * 2 * 3 = 6
- [2]
- Alternating Sum: 2
- Product: 2
The maximum product within the limit is 6.
Example 2:
Input: nums = [0,2,3], k = -5, limit = 12
Output: -1
Explanation:
A subsequence with an alternating sum of exactly -5 does not exist.
Example 3:
Input: nums = [2,2,3,3], k = 0, limit = 9
Output: 9
Explanation:
The subsequences with an alternating sum of 0 are:
- [2, 2]
- Alternating Sum: 2 - 2 = 0
- Product: 2 * 2 = 4
- [3, 3]
- Alternating Sum: 3 - 3 = 0
- Product: 3 * 3 = 9
- [2, 2, 3, 3]
- Alternating Sum: 2 - 2 + 3 - 3 = 0
- Product: 2 * 2 * 3 * 3 = 36
The subsequence [2, 2, 3, 3] has the greatest product with an alternating sum equal to k, but 36 > 9. The next greatest product is 9, which is within the limit.
Constraints:
1 <= nums.length <= 1500 <= nums[i] <= 12-10^5 <= k <= 10^51 <= limit <= 5000
Solution
Approach: Dynamic Programming with Bitmasks
The numbers are at most 12, so every alternating sum lies in [-S, S] where S is the sum of nums (at most 1800). The product only matters up to limit, so there are few distinct products worth tracking. A zero is the exception: a product that is already too big becomes 0 after multiplying by a zero, so all products above limit are kept together in one "too big" state (limit + 1).
State: (product, parity of the length) maps to a bitmask whose bit sum + S is set if some subsequence has that product, length parity and alternating sum. The parity tells where the next element lands: on an even index it is added (shift the mask left by x), on an odd index it is subtracted (shift right by x).
For every x in nums, in order:
- Keep all existing states (skip
x). - Extend every state: the product becomes
min(product * x, limit + 1), the parity flips and the mask is shifted byx. - Start a new subsequence
[x].
At the end, answer with the largest product <= limit whose mask has bit k + S set, or -1. If |k| > S the answer is -1 immediately.
Complexity: about O(n * P * S / w) where P is the number of distinct products and w the word size; in practice a few hundred thousand big-integer operations.
class Solution:
def maxProduct(self, nums: List[int], k: int, limit: int) -> int:
off = sum(nums) # every alternating sum lies in [-off, off]
if abs(k) > off:
return -1
# (product, parity of the length) -> bitmask of reachable alternating sums, bit (sum + off)
dp = {}
for x in nums:
new = dict(dp) # skip x
for (prod, par), mask in dp.items():
p = min(prod * x, limit + 1) # limit + 1 stands for "too big", a later 0 can still fix it
# x lands on an even index if the length so far is even, otherwise on an odd one
shifted = mask << x if par == 0 else mask >> x
key = (p, par ^ 1)
new[key] = new.get(key, 0) | shifted
key = (min(x, limit + 1), 1) # start a new subsequence with x
new[key] = new.get(key, 0) | (1 << (off + x))
dp = new
ans = -1
for (prod, par), mask in dp.items():
if prod <= limit and (mask >> (k + off)) & 1:
ans = max(ans, prod)
return ans