Watch 6 video solutions for Find the Sum of Subsequence Powers, a hard level problem involving Array, Dynamic Programming, Sorting. This walkthrough by Aryan Mittal has 3,373 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given an integer array nums of length n, and a positive integer k.
The power of a subsequence is defined as the minimum absolute difference between any two elements in the subsequence.
Return the sum of powers of all subsequences of nums which have length equal to k.
Since the answer may be large, return it modulo 109 + 7.
Example 1:
Input: nums = [1,2,3,4], k = 3
Output: 4
Explanation:
There are 4 subsequences in nums which have length 3: [1,2,3], [1,3,4], [1,2,4], and [2,3,4]. The sum of powers is |2 - 3| + |3 - 4| + |2 - 1| + |3 - 4| = 4.
Example 2:
Input: nums = [2,2], k = 2
Output: 0
Explanation:
The only subsequence in nums which has length 2 is [2,2]. The sum of powers is |2 - 2| = 0.
Example 3:
Input: nums = [4,3,-1], k = 2
Output: 10
Explanation:
There are 3 subsequences in nums which have length 2: [4,3], [4,-1], and [3,-1]. The sum of powers is |4 - 3| + |4 - (-1)| + |3 - (-1)| = 10.
Constraints:
2 <= n == nums.length <= 50-108 <= nums[i] <= 108 2 <= k <= nProblem Overview: You are given an array and must compute the total power of all valid subsequences. The power of a subsequence depends on the difference between elements after ordering, so you must carefully track element relationships while enumerating subsequences. A naive enumeration quickly becomes infeasible because the number of subsequences grows exponentially.
Approach 1: Dynamic Programming (O(n² * k) time, O(n * k) space)
Start by sorting the array so differences between elements can be processed in increasing order. Sorting allows you to compute the contribution of each pair or group of elements without recomputing gaps repeatedly. Use dynamic programming where dp[i][j] represents the number of ways to form subsequences of length j ending at index i. While extending subsequences, track the minimum difference between elements, which determines the power contribution.
Each new element attempts to extend subsequences that end before it. Iterate over previous indices and update DP states based on the difference between the current value and earlier values. The accumulated contribution of these differences builds the final answer. This method works well because sorting converts the problem into a structured progression of states instead of exploring every subsequence explicitly. It combines ideas from Dynamic Programming and Sorting to avoid exponential enumeration.
Approach 2: Kadane's Algorithm Inspired Optimization (O(n) time, O(1) space)
Some variations of the problem allow reducing the computation by observing that contributions of differences behave similarly to subarray accumulation. After sorting the array, treat the differences between consecutive elements as a sequence and accumulate contributions in a running manner. Similar to Kadane's Algorithm, maintain a running contribution and update the total whenever extending the current subsequence improves the cumulative power.
This approach works when the subsequence constraints allow linear accumulation of contributions. Instead of tracking all DP states, the algorithm maintains a running sum and updates it while scanning the array. The memory footprint drops to constant space, and the runtime becomes linear. The technique borrows the prefix accumulation idea commonly used in Array problems and Kadane-style maximum subarray logic.
Recommended for interviews: The dynamic programming solution is typically what interviewers expect. It demonstrates that you recognize the exponential subsequence space and replace it with structured DP states after sorting the input. Explaining the brute force idea first shows you understand the search space, but implementing the DP transition proves algorithmic maturity and control over time complexity.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming with Sorting | O(n² * k) | O(n * k) | General solution for computing contributions of all subsequences |
| Kadane-Inspired Accumulation | O(n) | O(1) | When subsequence power can be derived from consecutive differences |