Skip to content

Practice Longest Subsequence With Limited Sum

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2389
LeetCode Problem

Longest Subsequence With Limited Sum

Problem Statement

You are given an integer array nums of length n, and an integer array queries of length m. Return an array answer of length m where answer[i] is the maximum size of a subsequence that you can take from nums such that the sum of its elements is less than or equal to queries[i]. 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.

Source: Longest Subsequence With Limited Sum on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [4,5,2,1], queries = [3,10,21]
Output
[2,3,4]

# Example 2

Input
nums = [2,3,4,5], queries = [1]
Output
[0]

Constraints

n == nums.length
m == queries.length
1 <= n, m <= 1000
1 <= nums[i], queries[i] <= 10^6

Related LeetCode Problems

Practice more problems similar to Longest Subsequence With Limited Sum.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.