#2386
Find the K-Sum of an Array
master · 1670 · lc hard +32 · verified · 41.2% accepted · 608 likes · top 21%
Description
Given an integer array nums and a positive integer k, consider all possible subsequences of nums and their element sums. Define the K-Sum as the kth largest among all such subsequence sums (duplicates included in ranking).
Return the K-Sum of nums.
Example 1:
Input: nums = [2,4,-2], k = 5
Output: 2
Explanation: All the possible subsequence sums that we can obtain are the following sorted in decreasing order:
6, 4, 4, 2, 2, 0, 0, -2.
The 5-Sum of the array is 2.
Example 2:
Input: nums = [1,-2,3,4,-10,12], k = 16
Output: 10
Explanation: The 16-Sum of the array is 10.
Code
1
2
3