SalesforceOA #5
Closest Subsequence Sum
Estimated HardJudge-ready
Topics (AI-labelled)EnumerationBinary Search
You may delete any elements, including all or none of them; find the minimum absolute difference between the sum of the remaining subsequence and goal. Negative numbers and the empty subsequence are allowed.
Input
The first line contains n goal, and the second line contains n integers. 1≤n≤40, −10⁷≤nums[i]≤10⁷, −10⁹≤goal≤10⁹.
Output
Output the minimum absolute difference.
Example 1
Input:
4 6
5 -7 3 5
Output:
0
Example 2
Input:
4 -5
7 -9 15 -2
Output:
1
Example 3
Input:
3 -7
1 2 3
Output:
7
Example notes
The answers are 0, 1, and 7 in order; the third example requires allowing the empty subsequence.
Editorial
Loading solution…