Problem Description
Given an integer array nums of length n and a positive integer k, consider every subsequence (i.e. combination) of nums of length k. The “power” of a subsequence is defined as the minimum absolute difference between any two elements in that subsequence. Return the sum of the powers of all such subsequences modulo 10^9+7.
Key Insights
- Sorting the array makes it easier to reason about differences; in a sorted subsequence the minimum gap is simply the smallest difference between two consecutive elements.
- Instead of computing the power for each subsequence separately, observe that for a subsequence S the minimum gap can be represented by: power(S) = sum { for d >= 1 of indicator[S has all adjacent differences >= d] }.
- This lets us swap the order of summation: the answer equals sum_{d>=1} F(d) modulo MOD, where F(d) is the number of subsequences of length k with all adjacent gaps (in sorted order) at least d.
- Dynamic programming is used to count F(d) given a “gap threshold” d.
- F(d) is a non‐increasing step function (as d increases, fewer subsequences satisfy the condition) so we can compute it only at a few “breakpoints” and sum over intervals where F(d) remains constant.
Space and Time Complexity
Time Complexity: O(M * n^2) where M is the number of candidate gap thresholds (at most O(n)) and n ≤ 50. Space Complexity: O(n * k) for the recursion/memoization states.
Solution
The main idea is to first sort the array. For any chosen threshold d, count the number of k-length subsequences whose consecutive elements (in sorted order) differ by at least d using a recursive DP with memoization. Then, rather than summing F(d) for every integer d from 1 up to the maximum possible gap (which can be very large), we note that F(d) only changes when d reaches one of a small set of candidate values. In particular, the candidate thresholds include 1 and every distinct positive difference between consecutive elements in the sorted array. We then build intervals [L, R) over which F(d) remains constant and for each interval add count* (R-L) to the answer (all modulo 10^9+7).
The solution uses: • Sorting • Recursion with memoization (dynamic programming) to count valid subsequences under a gap constraint. • A sweep over candidate gap intervals.