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.
This approach leverages dynamic programming to break down the problem into subproblems, solving and combining them to build the solution.
The solution uses a dynamic programming technique to compute the maximum sum subarray. We maintain a dp array where dp[i] stores the maximum sum ending at index i. This helps to avoid recomputation and results in an optimal solution.
Time Complexity: O(n)
Space Complexity: O(n)
Kadane's algorithm offers an optimized way to solve the maximum subarray problem using only a constant amount of space.
This C solution uses Kadane's algorithm to efficiently find the maximum sum subarray. It iteratively calculates the current local maximum and updates the global maximum if the local maximum exceeds it.
Time Complexity: O(n)
Space Complexity: O(1)
Given the problem involves the minimum difference between elements of a subsequence, we might as well sort the array nums, which facilitates the calculation of the minimum difference between subsequence elements.
Next, we design a function dfs(i, j, k, mi), representing the value of the energy sum when processing the i-th element, the last selected element is the j-th element, k more elements need to be selected, and the current minimum difference is mi. Therefore, the answer is dfs(0, n, k, +infty) (If the last selected element is the n-th element, it indicates that no element has been selected before).
The execution process of the function dfs(i, j, k, mi) is as follows:
i geq n, it means all elements have been processed. If k = 0, return mi; otherwise, return 0.n - i is less than k, return 0.i-th element, and the energy sum obtained is dfs(i + 1, j, k, mi).i-th element. If j = n, it means no element has been selected before, then the energy sum obtained is dfs(i + 1, i, k - 1, mi); otherwise, the energy sum obtained is dfs(i + 1, i, k - 1, min(mi, nums[i] - nums[j])).10^9 + 7.To avoid repeated calculations, we can use memoization, saving the results that have already been calculated.
The time complexity is O(n^4 times k), and the space complexity is O(n^4 times k). Here, n is the length of the array.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Approach 1: Dynamic Programming | Time Complexity: O(n) |
| Approach 2: Kadane's Algorithm | Time Complexity: O(n) |
| Memoization Search | — |
| 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 |
3098. Find the Sum of Subsequence Powers | 4D DP | time- O(n^5) [*will be updated for O(n^4)] • Aryan Mittal • 3,373 views views
Watch 5 more video solutions →Practice Find the Sum of Subsequence Powers with our built-in code editor and test cases.
Practice on FleetCode