Home > front end >  Subset sum problem with continuous subset using recursion
Subset sum problem with continuous subset using recursion

Time:06-10

I am trying to think how to solve the Subset sum problem with an extra constraint: The subset of the array needs to be continuous (the indexes needs to be). I am trying to solve it using recursion in Java.

I know the solution for the non-constrained problem: Each element can be in the subset (and thus I perform a recursive call with sum = sum - arr[index]) or not be in it (and thus I perform a recursive call with sum = sum).

I am thinking about maybe adding another parameter for knowing weather or not the previous index is part of the subset, but I don't know what to do next.

CodePudding user response:

You are on the right track.

Think of it this way:

  • for every entry you have to decide: do you want to start a new sum at this point or skip it and reconsider the next entry.
  • a b c d contains the sum of b c d. Do you want to recompute the sums?
  • Maybe a bottom-up approach would be better

CodePudding user response:

The O(n) solution that you asked for:

This solution requires three fixed point numbers: The start and end indices, and the total sum of the span

Starting from element 0 (or from the end of the list if you want) increase the end index until the total sum is greater than or equal to the desired value. If it is equal, you've found a subset sum. If it is greater, move the start index up one and subtract the value of the previous start index. Finally, if the resulting total is greater than the desired value, move the end index back until the sum is less than the desired value. In the other case (where the sum is less) move the end index forward until the sum is greater than the desired value. If no match is found, repeat

So, caveats:

  1. Is this "fairly obvious"? Maybe, maybe not. I was making assumptions about order of magnitude similarity when I said both "fairly obvious" and o(n) in my comments
  2. Is this actually o(n)? It depends a lot on how similar (in terms of order of magnitude (digits in the number)) the numbers in the list are. The closer all the numbers are to each other, the fewer steps you'll need to make on the end index to test if a subset exists. On the other hand, if you have a couple of very big numbers (like in the thousands) surrounded by hundreds of pretty small numbers (1's and 2's and 3's) the solution I've presented will get closers to O(n^2)
  3. This solution only works based on your restriction that the subset values are continuous
  • Related