Problem: Given an integers array A[], length = n and a given integer value TARGET. Find the subsequence (might not be contiguous) satisfied that its sum is less than TARGET and nearest to that TARGET.
Eg.: A[] = [10, -2, 3, 7]. TARGET = 14. Answer = {10, 3}
I ran into this problem in a Hackerrank challenge, that I couldn't figure out any polynomial time solution, it first sounds quite familiar to some of dynamic programming problems, but this case the conditions 'No greater than' and 'nearest' seem eliminate that solution.
From my first thought following dynamic programming approach, at the i-problem (i=0->n-1), we need to evaluate all sub-sequences that either containing A[i] or not containing A[i], the latter is known as S[i-1], so just focus on all sub-sequences having A[i] as the last element.
That where we can't just rely on previous solved problems (0->i-1) as the sum we need must be less than and nearest to target might not be yielded from the smaller solutions, it might be produced from the second one, third one plus the last element A[i], and iterating over all subsequence containing A[i] would need to go through all 2^i - 1 subsets, excluding the single set {A[i]}.
Any suggestion on this kind of problem?