Practice Closest Subsequence Sum
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Closest Subsequence Sum
Problem Statement
You are given an integer array nums and an integer goal. You want to choose a subsequence of nums such that the sum of its elements is the closest possible to goal. That is, if the sum of the subsequence's elements is sum, then you want to minimize the absolute difference abs(sum - goal). Return the minimum possible value of abs(sum - goal). Note that a subsequence of an array is an array formed by removing some elements (possibly all or none) of the original array.
Source: Closest Subsequence Sum on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
nums = [5,-7,3,5], goal = 6
0
# Example 2
nums = [7,-9,15,-2], goal = -5
1
# Example 3
nums = [1,2,3], goal = -7
7
Constraints
Related LeetCode Problems
Practice more problems similar to Closest Subsequence Sum.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.