Skip to main content

Maximum Product of Subsequences With an Alternating Sum Equal to K - Solution & Explanation

HardArrayHash TableDynamic Programming4 min readAsked at: Microsoft
Practice this problem

Problem Statement

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 <= 150
  • 0 <= nums[i] <= 12
  • -105 <= k <= 105
  • 1 <= limit <= 5000

Approach Overview

Problem Overview: You are given an array and a target K. Choose a subsequence whose alternating sum (add the first element, subtract the second, add the third, and so on) equals K. Among all valid subsequences, return the maximum possible product of its elements.

Approach 1: Brute Force Subsequence Enumeration (O(2^n) time, O(n) space)

The direct method generates every subsequence and computes its alternating sum and product. For each subset, iterate through elements in order, flipping the sign based on index parity (+ - + -). If the final alternating sum equals K, update the maximum product. This approach uses recursion or bitmask enumeration and requires storing the current product and alternating sum during traversal. With 2^n possible subsequences, the method becomes impractical once n grows beyond small limits.

Approach 2: Dynamic Programming with Hash Map States (O(n * S) time, O(S) space)

A more practical solution tracks reachable alternating sums while scanning the array once. Maintain DP states keyed by (alternating_sum, parity), where parity indicates whether the next element contributes positively or negatively. Each state stores the maximum product achievable for that configuration. For every number, iterate through existing states and update two possibilities: include the element (update sum based on parity and multiply the product) or skip it. A hash map efficiently stores only reachable sums, which avoids scanning large unused ranges. This technique combines ideas from dynamic programming and hash tables while iterating through the array.

The key insight is that the alternating sign depends only on how many elements were already chosen. Tracking parity makes the alternating calculation constant time, and storing the best product per state avoids recomputing dominated results.

Approach 3: Pruned DP with State Compression (O(n * S) time, O(S) space)

The DP map can grow if many sums are reachable. You can prune dominated states by keeping only the maximum product for each (sum, parity). Any state producing a smaller product for the same configuration can be discarded because future multiplications will never outperform the larger one. This keeps the state space compact and improves practical performance while maintaining the same asymptotic complexity.

Recommended for interviews: Interviewers expect the dynamic programming approach with a hash map. Starting with the brute force method shows you understand the subsequence search space. Transitioning to DP demonstrates the key optimization: storing intermediate alternating sums and reusing them instead of recomputing all subsequences.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Subsequence EnumerationO(2^n)O(n)Small arrays or when demonstrating the baseline idea during interviews
Dynamic Programming with Hash MapO(n * S)O(S)General case where many subsequences exist but alternating sums can be reused
Pruned DP with State CompressionO(n * S)O(S)Large inputs where limiting dominated states improves practical performance

Video Solution

3509. Maximum Product of Subsequences With an Alternating Sum Equal to K • Tech Courses • 575 views views

Watch 2 more video solutions →

Frequently Asked Questions

Is Maximum Product of Subsequences With an Alternating Sum Equal to K easy or hard?
Maximum Product of Subsequences With an Alternating Sum Equal to K is classified as a Hard problem. It requires combining subsequence generation logic with dynamic programming and state compression, which makes it significantly more complex than standard array or hash map problems.
Maximum Product of Subsequences With an Alternating Sum Equal to K Python/Java solution
Implement the DP using a dictionary (Python) or HashMap (Java) keyed by (sum, parity). For each number, copy current states and update the alternating sum based on parity while multiplying the product. Keep only the maximum product per state to prevent the map from growing unnecessarily.
How to solve Maximum Product of Subsequences With an Alternating Sum Equal to K in O(n)?
A strict O(n) solution is generally not possible because the algorithm must track multiple alternating sums that can appear as subsequences are formed. The closest practical approach uses dynamic programming with a hash map and processes each element once while updating existing states. The complexity becomes O(n * S), where S is the number of reachable alternating sums.
What is the best approach for Maximum Product of Subsequences With an Alternating Sum Equal to K?
Dynamic programming with a hash map is the most effective approach. Track states using (alternating_sum, parity) and store the maximum product achievable for each state. As you iterate through the array, update states for including or skipping the current element. This avoids enumerating all subsequences and reduces the complexity to roughly O(n * S), where S is the number of reachable sums.
Is Maximum Product of Subsequences With an Alternating Sum Equal to K asked at Google/Amazon/Meta?
Problems combining subsequences, alternating sums, and dynamic programming are common in interviews at large tech companies such as Google, Amazon, and Meta. Variants frequently appear where candidates must track partial sums or parity while maximizing or minimizing another metric like product or length.
What data structure is used in Maximum Product of Subsequences With an Alternating Sum Equal to K?
The core data structures are a hash map for storing DP states and variables for tracking parity of the subsequence length. The hash map maps (alternating_sum, parity) to the maximum product achievable. This allows constant‑time updates and lookups while iterating through the array.
What is the time complexity of Maximum Product of Subsequences With an Alternating Sum Equal to K?
The optimized dynamic programming solution runs in about O(n * S) time, where S is the number of distinct alternating sums tracked in the hash map. Space complexity is O(S) because only reachable states are stored. A naive brute force approach requires O(2^n) time since it explores every subsequence.

Ready to solve this problem?

Practice Maximum Product of Subsequences With an Alternating Sum Equal to K with our built-in code editor and test cases.

Practice on FleetCode