BackhardSliding Window

Maximum Subsequence Value Solution

Problem Statement

Given an array of integers and two integers k and m, find the maximum sum of a subsequence of length k that can be obtained by removing at most m elements from the array and then selecting k elements from the remaining array

Example 1
Input
nums = [4, 2, 7, 1, 3], k = 3, m = 1
Output
14

Explanation: The optimal subsequence is [7, 4, 1] with sum 12, achieved by removing the element 2 and keeping the rest in the subsequence of length 3

Example 2
Input
nums = [9, 8, 5, 6, 4], k = 4, m = 2
Output
28

Explanation: The optimal subsequence is [9, 8, 6, 4] with sum 27, achieved by removing the element 5 from the array

Example 3
Input
nums = [1, 2, 3], k = 2, m = 0
Output
5

Explanation: The optimal subsequence is [2, 3] with sum 5

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= k <= nums.length
  • 0 <= m <= nums.length - k
  • -10^4 <= nums[i] <= 10^4
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free