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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an integer array nums and a positive integer k. Return the sum of the maximum and minimum elements of all subsequences of nums with at most k elements.
Since the answer may be very large, return it modulo 109 + 7.
Example 1:
Input: nums = [1,2,3], k = 2
Output: 24
Explanation:
The subsequences of nums with at most 2 elements are:
| Subsequence | Minimum | Maximum | Sum |
|---|---|---|---|
[1] |
1 | 1 | 2 |
[2] |
2 | 2 | 4 |
[3] |
3 | 3 | 6 |
[1, 2] |
1 | 2 | 3 |
[1, 3] |
1 | 3 | 4 |
[2, 3] |
2 | 3 | 5 |
| Final Total | 24 |
The output would be 24.
Example 2:
Input: nums = [5,0,6], k = 1
Output: 22
Explanation:
For subsequences with exactly 1 element, the minimum and maximum values are the element itself. Therefore, the total is 5 + 5 + 0 + 0 + 6 + 6 = 22.
Example 3:
Input: nums = [1,1,1], k = 2
Output: 12
Explanation:
The subsequences [1, 1] and [1] each appear 3 times. For all of them, the minimum and maximum are both 1. Thus, the total is 12.
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 1091 <= k <= min(70, nums.length)Problem summary: You are given an integer array nums and a positive integer k. Return the sum of the maximum and minimum elements of all subsequences of nums with at most k elements. Since the answer may be very large, return it modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Dynamic Programming
[1,2,3] 2
[5,0,6] 1
[1,1,1] 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3428: Maximum and Minimum Sums of at Most Size K Subsequences
class Solution {
public int minMaxSums(int[] nums, int k) {
// In a sorted array, nums[i] will be
// 1. The maximum for subsequences formed by nums[0..i].
// 2. The minimum for subsequences formed by nums[i..n - 1].
//
// The number of times nums[i] is the maximum is the same as the number of
// times nums[n - 1 - i] is the minimum, due to the symmetry in subsequences
// derived from the sorted order.
//
// To calculate the contribution of nums[i], we need to find the number of
// ways to select at most (k - 1) elements from the range of indices where
// nums[i] is the smallest or nums[n - 1 - i] is the largest.
final int n = nums.length;
final int[][] comb = getComb(n, k - 1);
long ans = 0;
Arrays.sort(nums);
// i: available numbers from the left of nums[i] or
// available numbers from the right of nums[n - 1 - i]
for (int i = 0; i < n; ++i) {
int count = 0;
for (int j = 0; j <= k - 1; ++j) // selected numbers
count = (count + comb[i][j]) % MOD;
ans += (long) nums[i] * count;
ans += (long) nums[n - 1 - i] * count;
ans %= MOD;
}
return (int) ans;
}
private static final int MOD = 1_000_000_007;
// C(n, k) = C(n - 1, k) + C(n - 1, k - 1)
private int[][] getComb(int n, int k) {
int[][] comb = new int[n + 1][k + 1];
for (int i = 0; i <= n; ++i)
comb[i][0] = 1;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= k; ++j)
comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % MOD;
return comb;
}
}
// Accepted solution for LeetCode #3428: Maximum and Minimum Sums of at Most Size K Subsequences
package main
import "slices"
// https://space.bilibili.com/206214
const mod = 1_000_000_007
const mx = 100_000
var f [mx]int // f[i] = i!
var invF [mx]int // invF[i] = i!^-1
func init() {
f[0] = 1
for i := 1; i < mx; i++ {
f[i] = f[i-1] * i % mod
}
invF[mx-1] = pow(f[mx-1], mod-2)
for i := mx - 1; i > 0; i-- {
invF[i-1] = invF[i] * i % mod
}
}
func pow(x, n int) int {
res := 1
for ; n > 0; n /= 2 {
if n%2 > 0 {
res = res * x % mod
}
x = x * x % mod
}
return res
}
func comb(n, m int) int {
if m > n {
return 0
}
return f[n] * invF[m] % mod * invF[n-m] % mod
}
func minMaxSums(nums []int, k int) (ans int) {
slices.Sort(nums)
s := 1
for i, x := range nums {
ans = (ans + s*(x+nums[len(nums)-1-i])) % mod
s = (s*2 - comb(i, k-1) + mod) % mod
}
return
}
# Accepted solution for LeetCode #3428: Maximum and Minimum Sums of at Most Size K Subsequences
class Solution:
def minMaxSums(self, nums: list[int], k: int) -> int:
# In a sorted array, nums[i] will be
# 1. The maximum for subsequences formed by nums[0..i].
# 2. The minimum for subsequences formed by nums[i..n - 1].
#
# The number of times nums[i] is the maximum is the same as the number of
# times nums[n - 1 - i] is the minimum, due to the symmetry in subsequences
# derived from the sorted order.
#
# To calculate the contribution of nums[i], we need to find the number of
# ways to select at most (k - 1) elements from the range of indices where
# nums[i] is the smallest or nums[n - 1 - i] is the largest.
MOD = 1_000_000_007
n = len(nums)
def getComb(n: int, k: int) -> list[list[int]]:
"""C(n, k) = C(n - 1, k) + C(n - 1, k - 1)"""
comb = [[0] * (k + 1) for _ in range(n + 1)]
for i in range(n + 1):
comb[i][0] = 1
for i in range(1, n + 1):
for j in range(1, k + 1):
comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % MOD
return comb
comb = getComb(n, k - 1)
ans = 0
nums.sort()
# i: available numbers from the left of nums[i] or
# available numbers from the right of nums[-1 - i]
for i in range(n):
count = 0
for j in range(k): # selected numbers
count = (count + comb[i][j]) % MOD
ans += nums[i] * count
ans += nums[-1 - i] * count
ans %= MOD
return ans
// Accepted solution for LeetCode #3428: Maximum and Minimum Sums of at Most Size K Subsequences
// 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 #3428: Maximum and Minimum Sums of at Most Size K Subsequences
// class Solution {
// public int minMaxSums(int[] nums, int k) {
// // In a sorted array, nums[i] will be
// // 1. The maximum for subsequences formed by nums[0..i].
// // 2. The minimum for subsequences formed by nums[i..n - 1].
// //
// // The number of times nums[i] is the maximum is the same as the number of
// // times nums[n - 1 - i] is the minimum, due to the symmetry in subsequences
// // derived from the sorted order.
// //
// // To calculate the contribution of nums[i], we need to find the number of
// // ways to select at most (k - 1) elements from the range of indices where
// // nums[i] is the smallest or nums[n - 1 - i] is the largest.
// final int n = nums.length;
// final int[][] comb = getComb(n, k - 1);
// long ans = 0;
//
// Arrays.sort(nums);
//
// // i: available numbers from the left of nums[i] or
// // available numbers from the right of nums[n - 1 - i]
// for (int i = 0; i < n; ++i) {
// int count = 0;
// for (int j = 0; j <= k - 1; ++j) // selected numbers
// count = (count + comb[i][j]) % MOD;
// ans += (long) nums[i] * count;
// ans += (long) nums[n - 1 - i] * count;
// ans %= MOD;
// }
//
// return (int) ans;
// }
//
// private static final int MOD = 1_000_000_007;
//
// // C(n, k) = C(n - 1, k) + C(n - 1, k - 1)
// private int[][] getComb(int n, int k) {
// int[][] comb = new int[n + 1][k + 1];
// for (int i = 0; i <= n; ++i)
// comb[i][0] = 1;
// for (int i = 1; i <= n; ++i)
// for (int j = 1; j <= k; ++j)
// comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % MOD;
// return comb;
// }
// }
// Accepted solution for LeetCode #3428: Maximum and Minimum Sums of at Most Size K Subsequences
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3428: Maximum and Minimum Sums of at Most Size K Subsequences
// class Solution {
// public int minMaxSums(int[] nums, int k) {
// // In a sorted array, nums[i] will be
// // 1. The maximum for subsequences formed by nums[0..i].
// // 2. The minimum for subsequences formed by nums[i..n - 1].
// //
// // The number of times nums[i] is the maximum is the same as the number of
// // times nums[n - 1 - i] is the minimum, due to the symmetry in subsequences
// // derived from the sorted order.
// //
// // To calculate the contribution of nums[i], we need to find the number of
// // ways to select at most (k - 1) elements from the range of indices where
// // nums[i] is the smallest or nums[n - 1 - i] is the largest.
// final int n = nums.length;
// final int[][] comb = getComb(n, k - 1);
// long ans = 0;
//
// Arrays.sort(nums);
//
// // i: available numbers from the left of nums[i] or
// // available numbers from the right of nums[n - 1 - i]
// for (int i = 0; i < n; ++i) {
// int count = 0;
// for (int j = 0; j <= k - 1; ++j) // selected numbers
// count = (count + comb[i][j]) % MOD;
// ans += (long) nums[i] * count;
// ans += (long) nums[n - 1 - i] * count;
// ans %= MOD;
// }
//
// return (int) ans;
// }
//
// private static final int MOD = 1_000_000_007;
//
// // C(n, k) = C(n - 1, k) + C(n - 1, k - 1)
// private int[][] getComb(int n, int k) {
// int[][] comb = new int[n + 1][k + 1];
// for (int i = 0; i <= n; ++i)
// comb[i][0] = 1;
// for (int i = 1; i <= n; ++i)
// for (int j = 1; j <= k; ++j)
// comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % MOD;
// return comb;
// }
// }
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: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
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.