2386. Find the K-Sum of an Array Hard
1/**
2 * [2386] Find the K-Sum of an Array
3 *
4 * You are given an integer array nums and a positive integer k. You can choose any subsequence of the array and sum all of its elements together.
5 * We define the K-Sum of the array as the k^th largest subsequence sum that can be obtained (not necessarily distinct).
6 * Return the K-Sum of the array.
7 * A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
8 * Note that the empty subsequence is considered to have a sum of 0.
9 *
10 * Example 1:
11 *
12 * Input: nums = [2,4,-2], k = 5
13 * Output: 2
14 * Explanation: All the possible subsequence sums that we can obtain are the following sorted in decreasing order:
15 * - 6, 4, 4, 2, <u>2</u>, 0, 0, -2.
16 * The 5-Sum of the array is 2.
17 *
18 * Example 2:
19 *
20 * Input: nums = [1,-2,3,4,-10,12], k = 16
21 * Output: 10
22 * Explanation: The 16-Sum of the array is 10.
23 *
24 *
25 * Constraints:
26 *
27 * n == nums.length
28 * 1 <= n <= 10^5
29 * -10^9 <= nums[i] <= 10^9
30 * 1 <= k <= min(2000, 2^n)
31 *
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/find-the-k-sum-of-an-array/
36// discuss: https://leetcode.com/problems/find-the-k-sum-of-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41 pub fn k_sum(nums: Vec<i32>, k: i32) -> i64 {
42
43 }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 #[test]
53 fn test_2386() {
54 }
55}
56
Back
© 2025 bowen.ge All Rights Reserved.