2

I got X binary numbers of length Y and want to see if they add up to a specific sum K.

I did some research on dynamic solutions for subset sum problems; however, I think this specific problem presents a twist.

Adding two binary numbers with length Y and restricting the sum's length to Y will sometimes subtract 2^(Y) from the sum (if there is overflow). Since adding two binary numbers sometimes causes overflow, such as adding:

10000000000000000000000000000000000000010
10000000000000000000000000000000000000010

Will yield the sum

00000000000000000000000000000000000000100

Therefore, in some cases adding numbers will actually lower the current sum.

At the moment, I am banging my head into a wall. Any tips or pointers how to attack this specific version of the subset sum problem?

UPDATE:

There are no real limitations to what I can use. My only goal is to produce the fastest possible run-time with ~50 binary numbers of length ~40

RasmusJ
  • 396
  • 1
  • 2
  • 10
  • Why does it matter how the numbers are expressed? What you have is still some version of the subset sum problem. So, what are the additional constraints here which will make it solvable faster? How many numbers can you have? – Pradhan Dec 26 '14 at 19:10
  • As far as I know this shouldn't matter, integers modulo a power of two are a [ring](http://en.wikipedia.org/wiki/Ring_%28mathematics%29) instead of a [field](http://en.wikipedia.org/wiki/Field_(mathematics)), but none of the subset sum algorithms I know about rely on the existence of multiplicative inverses. – harold Dec 26 '14 at 19:13
  • Using a modulo 2^(Y+1) wont disrupt the standard dynamic-programming algorithm? – RasmusJ Dec 26 '14 at 19:20
  • No, but it will require an array of length 2^Y (btw isn't it modulo 2^Y?) – harold Dec 26 '14 at 19:23
  • You're correct, it is 2^Y. However, arrays of length 2^Y when Y = ~40 in my average run-case seems extreme. Is this the only solution? – RasmusJ Dec 26 '14 at 19:32
  • You could try hash sets, since most entries should be false. Or you could try a different approach. – harold Dec 26 '14 at 19:34

1 Answers1

2

You can use meet-in-the middle technique.

  1. Split your initial array into to halves(with 25 numbers if there are 50 numbers in total).

  2. For each half, generate all possible subset sums(it takes something likeO(2^n) or O(2^n * n) time). It should feasible for n = 25(n is the size of one half).

  3. For each possible sum from the first half, check if there is an appropriate sum in the second half using hash table(using the fact that A + B = target (mod M) means B = target - A (mod M)).

kraskevich
  • 18,368
  • 4
  • 33
  • 45
  • Wouldn't this result in O(n^2) solution which also requires O(n^2) memory? Perhaps it is the only way, but it is not any big step up from the brute solution (simply generating all possible subsets in O(n^2) time). – RasmusJ Dec 26 '14 at 20:05
  • @RasmusJ It is `O(2 ^ (n / 2))` time and memory. It is much better than `O(2 ^ n)`. – kraskevich Dec 26 '14 at 20:11