There are n values given. Is there a possibility to have two Arrays A and B each containing elements from the given values such that the sum of all elements of A minus the sum of all elements of B gives the searched value? Each value can be used twice, but not twice in the same subset, so once in A and once in B at most (not necessary to use all the values in either A or B)

I was thinking of calculating every subsum and then, if one of the subsums is greater than the sum we want to reach, we search another subsum such that if we subtract it from the first sum we reach the searched value.

I don't think my idea is very efficient, nor dynamic. Any ideas how this problem could be solved dynamically?

  • $\begingroup$ For future reference, if you need to make changes, rather than deleting your post and re-posting it, we'd prefer if you use the 'edit' button under the question to edit the question. All is well now, but just wanted to let you know in case it arises again in the future. Thank you! $\endgroup$ – D.W. Nov 23 '17 at 21:45
  • $\begingroup$ You can adjust the following algorithm to solve with your problem: en.wikipedia.org/wiki/… $\endgroup$ – D.W. Nov 23 '17 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.