-1

The problem is:

Suppose that you are given an algorithm as a black box – you cannot see how it is designed – it has the following properties: if you input any sequence of real numbers and an integer k, the algorithm will answer YES or NO indicating whether there is a subset of numbers whose sum is exactly k. Show how to use this black box to find the subset of a given set of numbers S whose sum is k. You can use the black box O(n) times.

Before I post this question, I've already searched for the exact same question:

An algorithm to determine a subset sequence in O(n)?

Using subset-sum oracle to determine which numbers are members of the subset

https://cs.stackexchange.com/questions/14270/finding-the-subset-of-s-that-sums-up-to-k-using-a-black-box-in-on-time

I cannot find a satisfactory answer in those posts. Suppose

S = {1, 2, 3, 4, 5, 6, 7, 8}, k = 7

Querying Oracle (S or S \setminus any one element), will return YES. that doesn't help in coming up with a particular subset that sum up to k.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jamesszm
  • 101
  • 1
  • 10
  • So, there are three duplicate threads, and all you have tried is passing the entire sum only? Absolutely no other idea comes to your mind? – vgru Oct 09 '16 at 19:58
  • Which part of the first link is not helpful? Also note that there can be multiple subsets that sum up to k and it doesn't matter which one you find. – Keiwan Oct 09 '16 at 19:59
  • I've explained why I think it's not helpful.. it's my last sentence. let's say I make 8 queries: Oractle ( S \setminus 1 element ), getting 8 YES. that tells me, every single element of S doesn't need to be in the subset that sums to k. that's doesn't help. – Jamesszm Oct 09 '16 at 20:02
  • Yes, if you keep all of the elements in the list it doesn't. But with every query you know something about the element that you left out of the current query, namely whether or not it is needed to form a subset that sums up to k. If it's not needed then remove it and do more queries with the rest, if it is needed you add this number to your solution set and proceed with the rest of the elements left in S. – Keiwan Oct 09 '16 at 20:06
  • This is also why I wrote that you need to keep in mind that there can be multiple solutions, but you only need to find one. So in your example no number *needs* to be in the subset, because there are multiple solutions, but if you remove them one by one you`ll find one possible solution eventually. – Keiwan Oct 09 '16 at 20:08

1 Answers1

1

For all i [0 : n - 1], run the sequence without the ith element through the box. If it returns YES, keep the element removed and resume, if NO, return the element and resume. Done.

Amit
  • 45,440
  • 9
  • 78
  • 110