Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,2,3,4] 3
[2,2] 2
[4,3,-1] 2
number-of-subsequences-that-satisfy-the-given-sum-condition)closest-subsequence-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3098: Find the Sum of Subsequence Powers
class Solution {
private Map<Long, Integer> f = new HashMap<>();
private final int mod = (int) 1e9 + 7;
private int[] nums;
public int sumOfPowers(int[] nums, int k) {
Arrays.sort(nums);
this.nums = nums;
return dfs(0, nums.length, k, Integer.MAX_VALUE);
}
private int dfs(int i, int j, int k, int mi) {
if (i >= nums.length) {
return k == 0 ? mi : 0;
}
if (nums.length - i < k) {
return 0;
}
long key = (1L * mi) << 18 | (i << 12) | (j << 6) | k;
if (f.containsKey(key)) {
return f.get(key);
}
int ans = dfs(i + 1, j, k, mi);
if (j == nums.length) {
ans += dfs(i + 1, i, k - 1, mi);
} else {
ans += dfs(i + 1, i, k - 1, Math.min(mi, nums[i] - nums[j]));
}
ans %= mod;
f.put(key, ans);
return ans;
}
}
// Accepted solution for LeetCode #3098: Find the Sum of Subsequence Powers
func sumOfPowers(nums []int, k int) int {
const mod int = 1e9 + 7
sort.Ints(nums)
n := len(nums)
f := map[int]int{}
var dfs func(i, j, k, mi int) int
dfs = func(i, j, k, mi int) int {
if i >= n {
if k == 0 {
return mi
}
return 0
}
if n-i < k {
return 0
}
key := mi<<18 | (i << 12) | (j << 6) | k
if v, ok := f[key]; ok {
return v
}
ans := dfs(i+1, j, k, mi)
if j == n {
ans += dfs(i+1, i, k-1, mi)
} else {
ans += dfs(i+1, i, k-1, min(mi, nums[i]-nums[j]))
}
ans %= mod
f[key] = ans
return ans
}
return dfs(0, n, k, math.MaxInt)
}
# Accepted solution for LeetCode #3098: Find the Sum of Subsequence Powers
class Solution:
def sumOfPowers(self, nums: List[int], k: int) -> int:
@cache
def dfs(i: int, j: int, k: int, mi: int) -> int:
if i >= n:
return mi if k == 0 else 0
if n - i < k:
return 0
ans = dfs(i + 1, j, k, mi)
if j == n:
ans += dfs(i + 1, i, k - 1, mi)
else:
ans += dfs(i + 1, i, k - 1, min(mi, nums[i] - nums[j]))
ans %= mod
return ans
mod = 10**9 + 7
n = len(nums)
nums.sort()
return dfs(0, n, k, inf)
// Accepted solution for LeetCode #3098: Find the Sum of Subsequence Powers
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3098: Find the Sum of Subsequence Powers
// class Solution {
// private Map<Long, Integer> f = new HashMap<>();
// private final int mod = (int) 1e9 + 7;
// private int[] nums;
//
// public int sumOfPowers(int[] nums, int k) {
// Arrays.sort(nums);
// this.nums = nums;
// return dfs(0, nums.length, k, Integer.MAX_VALUE);
// }
//
// private int dfs(int i, int j, int k, int mi) {
// if (i >= nums.length) {
// return k == 0 ? mi : 0;
// }
// if (nums.length - i < k) {
// return 0;
// }
// long key = (1L * mi) << 18 | (i << 12) | (j << 6) | k;
// if (f.containsKey(key)) {
// return f.get(key);
// }
// int ans = dfs(i + 1, j, k, mi);
// if (j == nums.length) {
// ans += dfs(i + 1, i, k - 1, mi);
// } else {
// ans += dfs(i + 1, i, k - 1, Math.min(mi, nums[i] - nums[j]));
// }
// ans %= mod;
// f.put(key, ans);
// return ans;
// }
// }
// Accepted solution for LeetCode #3098: Find the Sum of Subsequence Powers
function sumOfPowers(nums: number[], k: number): number {
const mod = BigInt(1e9 + 7);
nums.sort((a, b) => a - b);
const n = nums.length;
const f: Map<bigint, bigint> = new Map();
function dfs(i: number, j: number, k: number, mi: number): bigint {
if (i >= n) {
if (k === 0) {
return BigInt(mi);
}
return BigInt(0);
}
if (n - i < k) {
return BigInt(0);
}
const key =
(BigInt(mi) << BigInt(18)) |
(BigInt(i) << BigInt(12)) |
(BigInt(j) << BigInt(6)) |
BigInt(k);
if (f.has(key)) {
return f.get(key)!;
}
let ans = dfs(i + 1, j, k, mi);
if (j === n) {
ans += dfs(i + 1, i, k - 1, mi);
} else {
ans += dfs(i + 1, i, k - 1, Math.min(mi, nums[i] - nums[j]));
}
ans %= mod;
f.set(key, ans);
return ans;
}
return Number(dfs(0, n, k, Number.MAX_SAFE_INTEGER));
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.