Skip to content

Practice Length of the Longest Subsequence That Sums to Target

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

Medium#2915
LeetCode Problem

Length of the Longest Subsequence That Sums to Target

Problem Statement

You are given a 0-indexed array of integers nums, and an integer target. Return the length of the longest subsequence of nums that sums up to target. If no such subsequence exists, return -1. 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: Length of the Longest Subsequence That Sums to Target on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,2,3,4,5], target = 9
Output
3

# Example 2

Input
nums = [4,1,3,2,1,5], target = 7
Output
4

# Example 3

Input
nums = [1,1,5,4,5], target = 3
Output
-1

Constraints

1 <= nums.length <= 1000
1 <= nums[i] <= 1000
1 <= target <= 1000

Related LeetCode Problems

Practice more problems similar to Length of the Longest Subsequence That Sums to Target.

Review your practice with PhantomCodeAI

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