3098. Find the Sum of Subsequence Powers Hard
1/**
2 * [3098] Find the Sum of Subsequence Powers
3 *
4 * You are given an integer array nums of length n, and a positive integer k.
5 * The power of a <span data-keyword="subsequence-array">subsequence</span> is defined as the minimum absolute difference between any two elements in the subsequence.
6 * Return the sum of powers of all subsequences of nums which have length equal to k.
7 * Since the answer may be large, return it modulo 10^9 + 7.
8 *
9 * <strong class="example">Example 1:
10 * <div class="example-block">
11 * Input: <span class="example-io">nums = [1,2,3,4], k = 3</span>
12 * Output: <span class="example-io">4</span>
13 * Explanation:
14 * 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.
15 * </div>
16 * <strong class="example">Example 2:
17 * <div class="example-block">
18 * Input: <span class="example-io">nums = [2,2], k = 2</span>
19 * Output: <span class="example-io">0</span>
20 * Explanation:
21 * The only subsequence in nums which has length 2 is [2,2]. The sum of powers is |2 - 2| = 0.
22 * </div>
23 * <strong class="example">Example 3:
24 * <div class="example-block">
25 * Input: <span class="example-io">nums = [4,3,-1], k = 2</span>
26 * Output: <span class="example-io">10</span>
27 * Explanation:
28 * 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.
29 * </div>
30 *
31 * Constraints:
32 *
33 * 2 <= n == nums.length <= 50
34 * -10^8 <= nums[i] <= 10^8
35 * 2 <= k <= n
36 *
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/find-the-sum-of-subsequence-powers/
41// discuss: https://leetcode.com/problems/find-the-sum-of-subsequence-powers/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46 pub fn sum_of_powers(nums: Vec<i32>, k: i32) -> i32 {
47 0
48 }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn test_3098() {
59 }
60}
61
Back
© 2025 bowen.ge All Rights Reserved.