2099. Find Subsequence of Length K With the Largest Sum LeetCode Solution

In this guide, you will get 2099. Find Subsequence of Length K With the Largest Sum LeetCode Solution with the best time and space complexity. The solution to Find Subsequence of Length K With the Largest Sum problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Subsequence of Length K With the Largest Sum solution in C++
  4. Find Subsequence of Length K With the Largest Sum solution in Java
  5. Find Subsequence of Length K With the Largest Sum solution in Python
  6. Additional Resources
2099. Find Subsequence of Length K With the Largest Sum LeetCode Solution image

Problem Statement of Find Subsequence of Length K With the Largest Sum

You are given an integer array nums and an integer k. You want to find a subsequence of nums of length k that has the largest sum.
Return any such subsequence as an integer array of length k.
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.

Example 1:

Input: nums = [2,1,3,3], k = 2
Output: [3,3]
Explanation:
The subsequence has the largest sum of 3 + 3 = 6.
Example 2:

Input: nums = [-1,-2,3,4], k = 3
Output: [-1,3,4]
Explanation:
The subsequence has the largest sum of -1 + 3 + 4 = 6.

Example 3:

Input: nums = [3,4,3,3], k = 2
Output: [3,4]
Explanation:
The subsequence has the largest sum of 3 + 4 = 7.
Another possible subsequence is [4, 3].

Constraints:

1 <= nums.length <= 1000
-105 <= nums[i] <= 105
1 <= k <= nums.length

Complexity Analysis

  • Time Complexity: O(n) \to O(n\log n)
  • Space Complexity: O(n)

2099. Find Subsequence of Length K With the Largest Sum LeetCode Solution in C++

class Solution {
 public:
  vector<int> maxSubsequence(vector<int>& nums, int k) {
    vector<int> ans;
    vector<int> arr(nums);
    nth_element(arr.begin(), arr.end() - k, arr.end());
    const int threshold = arr[arr.size() - k];
    const int larger =
        ranges::count_if(nums, [&](int num) { return num > threshold; });
    int equal = k - larger;

    for (const int num : nums)
      if (num > threshold) {
        ans.push_back(num);
      } else if (num == threshold && equal) {
        ans.push_back(num);
        --equal;
      }

    return ans;
  }
};
/* code provided by PROGIEZ */

2099. Find Subsequence of Length K With the Largest Sum LeetCode Solution in Java

class Solution {
  public int[] maxSubsequence(int[] nums, int k) {
    int[] ans = new int[k];
    int[] arr = nums.clone();
    Arrays.sort(arr);
    final int threshold = arr[arr.length - k];
    final int larger = (int) Arrays.stream(nums).filter(num -> num > threshold).count();
    int equal = k - larger;

    int i = 0;
    for (final int num : nums)
      if (num > threshold) {
        ans[i++] = num;
      } else if (num == threshold && equal > 0) {
        ans[i++] = num;
        --equal;
      }

    return ans;
  }
}
// code provided by PROGIEZ

2099. Find Subsequence of Length K With the Largest Sum LeetCode Solution in Python

class Solution:
  def maxSubsequence(self, nums: list[int], k: int) -> list[int]:
    ans = []
    threshold = sorted(nums)[-k]
    larger = sum(num > threshold for num in nums)
    equal = k - larger

    for num in nums:
      if num > threshold:
        ans.append(num)
      elif num == threshold and equal:
        ans.append(num)
        equal -= 1

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.