Longest Subsequence With Limited Sum
Try to solve the Longest Subsequence With Limited Sum problem.
We'll cover the following
Statement
You are given an integer array, nums
, of length n
, and an integer array, queries
, of length m
.
For each element in queries
, determine the maximum number of elements that can be selected from nums
to form a
Return an array answer
of length m
, where answer[i]
represents the size of the largest subsequence of nums
whose sum is less than or equal to queries[i]
.
Constraints
n
nums.length
m
queries.length
n
,m
nums[i]
,queries[i]
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Longest Subsequence With Limited Sum
What is the output for the inputs given below?
nums = [1, 3, 4, 6, 15]
queries = [5, 10, 20]
[2, 3, 4]
[1, 2, 4]
[2, 3, 5]
[1, 2, 3]
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in the following coding playground.
import java.util.*;public class Solution{public static int[] answerQueries(int[] nums, int[] queries) {// Replace this placeholder return statement with your codereturn new int[]{};}}
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.