Let
X,Ysubsets of{1,...,100n}where|X|=3nand|Y|=7n. FindAsubset ofXandBsubset ofYsuch that: both are not empty,|A|=|B|andsum a_i = sum b_i.
- There are
O(n^2)sums we can make of the set\{1,...,100n\}(the largest one is1+... +100neven though this one isn't possible sinceXandYdon't include all numbers) - There are
O(n)cardinals (set sizes) for every sum (from the previous bullet) - We can have a table of
\{0,1\}(booleans) with the size ofO(n) X O(n^2)where rows represents cardinality and columns represents a number. So1means we can create a subset with cardinalityiand the sum of the set isj - First row/column is easy to calculate of course
Now basically I need to iterate all cells and update them in O(n) per cell. So we shall end-up with an overall time-complexity of O(n^4).
How can I do that?
I think I could iterate it row by row; Meaning, if I want to update the cell T[i,j] (Meaning, a set with a size i of sum j), then I could look for a set of size i-1 plus some term which equals together j.
BUT! It could be that we already used this term in the previous set (of size i-1) - Problem!