java.lang.Object
g2301_2400.s2386_find_the_k_sum_of_an_array.Solution

public class Solution extends Object
2386 - Find the K-Sum of an Array.<p>Hard</p> <p>You are given an integer array <code>nums</code> and a <strong>positive</strong> integer <code>k</code>. You can choose any <strong>subsequence</strong> of the array and sum all of its elements together.</p> <p>We define the <strong>K-Sum</strong> of the array as the <code>k<sup>th</sup></code> <strong>largest</strong> subsequence sum that can be obtained ( <strong>not</strong> necessarily distinct).</p> <p>Return <em>the K-Sum of the array</em>.</p> <p>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</p> <p><strong>Note</strong> that the empty subsequence is considered to have a sum of <code>0</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [2,4,-2], k = 5</p> <p><strong>Output:</strong> 2</p> <p><strong>Explanation:</strong> All the possible subsequence sums that we can obtain are the following sorted in decreasing order:</p> <ul> <li>6, 4, 4, 2, <ins>2</ins>, 0, 0, -2.</li> </ul> <p>The 5-Sum of the array is 2.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,-2,3,4,-10,12], k = 16</p> <p><strong>Output:</strong> 10</p> <p><strong>Explanation:</strong> The 16-Sum of the array is 10.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 <= n <= 10<sup>5</sup></code></li> <li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li> <li><code>1 <= k <= min(2000, 2<sup>n</sup>)</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • kSum

      public long kSum(int[] nums, int k)