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 <= n
def solve():
nums = [4, 3, -1]
k = 2
print(f"Input: nums = {nums}, k = {k}")
n = len(nums)
subsequences = []
def find_subsequences(index, current_subsequence):
if len(current_subsequence) == k:
subsequences.append(current_subsequence[:])
return
if index == n:
return
# Include the current element
current_subsequence.append(nums[index])
find_subsequences(index + 1, current_subsequence)
current_subsequence.pop()
# Exclude the current element
find_subsequences(index + 1, current_subsequence)
find_subsequences(0, [])
total_power = 0
for subsequence in subsequences:
min_diff = float('inf')
for i in range(len(subsequence)):
for j in range(i + 1, len(subsequence)):
min_diff = min(min_diff, abs(subsequence[i] - subsequence[j]))
total_power += min_diff
print(f"Output: {total_power}")
print(f"Explanation:")
print(f"There are {len(subsequences)} subsequences in nums which have length 2: {[str(sub) for sub in subsequences]}. The sum of powers is calculated as the sum of the minimum absolute differences between elements in each subsequence.")
print("\n")
nums = [1, 2, 3, 4]
k = 3
print(f"Input: nums = {nums}, k = {k}")
n = len(nums)
subsequences = []
def find_subsequences(index, current_subsequence):
if len(current_subsequence) == k:
subsequences.append(current_subsequence[:])
return
if index == n:
return
# Include the current element
current_subsequence.append(nums[index])
find_subsequences(index + 1, current_subsequence)
current_subsequence.pop()
# Exclude the current element
find_subsequences(index + 1, current_subsequence)
find_subsequences(0, [])
total_power = 0
for subsequence in subsequences:
min_diff = float('inf')
for i in range(len(subsequence)):
for j in range(i + 1, len(subsequence)):
min_diff = min(min_diff, abs(subsequence[i] - subsequence[j]))
total_power += min_diff
print(f"Output: {total_power}")
print(f"Explanation:")
print(f"There are {len(subsequences)} subsequences in nums which have length 3: {[str(sub) for sub in subsequences]}. The sum of powers is calculated as the sum of the minimum absolute differences between elements in each subsequence.")
print("\n")
nums = [2, 2]
k = 2
print(f"Input: nums = {nums}, k = {k}")
n = len(nums)
subsequences = []
def find_subsequences(index, current_subsequence):
if len(current_subsequence) == k:
subsequences.append(current_subsequence[:])
return
if index == n:
return
# Include the current element
current_subsequence.append(nums[index])
find_subsequences(index + 1, current_subsequence)
current_subsequence.pop()
# Exclude the current element
find_subsequences(index + 1, current_subsequence)
find_subsequences(0, [])
total_power = 0
for subsequence in subsequences:
min_diff = float('inf')
for i in range(len(subsequence)):
for j in range(i + 1, len(subsequence)):
min_diff = min(min_diff, abs(subsequence[i] - subsequence[j]))
total_power += min_diff
print(f"Output: {total_power}")
print(f"Explanation:")
print(f"There is {len(subsequences)} subsequence in nums which has length 2: {[str(sub) for sub in subsequences]}. The sum of powers is calculated as the sum of the minimum absolute differences between elements in each subsequence.")
solve()
The provided Python code implements a brute-force solution to the problem. It first generates all possible subsequences of length k
from the input array nums
. Then, for each subsequence, it computes the minimum absolute difference between any two elements. Finally, it sums up these minimum differences across all subsequences and returns the result.
Here's a breakdown:
find_subsequences
generates all subsequences of length k
. It explores all possible combinations of elements by either including or excluding each element in the input array.To avoid recomputing the subsequences, we could compute the number of times a particular pair of elements would be present in a subsequence of size k
. Let's say we sort the array. Then, for each pair (nums[i], nums[j]) where i < j, we compute how many subsequences of size k contain both nums[i] and nums[j]. The remaining positions are k - 2
positions, and these can be filled by any of the remaining n - 2
elements. Thus, there are C(n-2, k-2)
such subsequences. So the contribution of this pair to the final answer is abs(nums[i] - nums[j]) * C(n-2, k-2)
. So the overall complexity would be O(n^2) for going through each pair, and O(k) to compute the combination using dynamic programming.
def solve_optimal():
nums = [4, 3, -1]
k = 2
print(f"Input: nums = {nums}, k = {k}")
n = len(nums)
nums.sort()
MOD = 10**9 + 7
def combinations(n, k, mod):
if k < 0 or k > n:
return 0
if k == 0 or k == n:
return 1
if k > n // 2:
k = n - k
dp = [0] * (k + 1)
dp[0] = 1
for i in range(1, n + 1):
for j in range(min(i, k), 0, -1):
dp[j] = (dp[j] + dp[j-1]) % mod
return dp[k]
total_power = 0
num_combinations = combinations(n - 2, k - 2, MOD)
for i in range(n):
for j in range(i + 1, n):
total_power = (total_power + abs(nums[i] - nums[j]) * num_combinations) % MOD
print(f"Output: {total_power}")
nums = [1, 2, 3, 4]
k = 3
print(f"Input: nums = {nums}, k = {k}")
n = len(nums)
nums.sort()
total_power = 0
num_combinations = combinations(n - 2, k - 2, MOD)
for i in range(n):
for j in range(i + 1, n):
total_power = (total_power + abs(nums[i] - nums[j]) * num_combinations) % MOD
print(f"Output: {total_power}")
nums = [2, 2]
k = 2
print(f"Input: nums = {nums}, k = {k}")
n = len(nums)
nums.sort()
total_power = 0
num_combinations = combinations(n - 2, k - 2, MOD)
for i in range(n):
for j in range(i + 1, n):
total_power = (total_power + abs(nums[i] - nums[j]) * num_combinations) % MOD
print(f"Output: {total_power}")
solve_optimal()
find_subsequences
function explores all possible subsequences, which is 2n in the worst case. Thus, the time complexity is O(2n).subsequences
list can store up to O(2n) subsequences in the worst case.find_subsequences
can go as deep as O(n) in the call stack.