I have a set of integers like this
{1,4,5,2,7,8,-3,-5,-6,9,3,-7,-1,5,6}
the set can contain any number of items as input is taken from the
I came across this problem in my college days at Google interview and solved it in a very long way.
Think about it, for a set to be 0 there "has" to be negative number and there "has to be a set of positive number".
Steps:
1. Created a 2 arrays negativeNumArrays and POsitiveNumArrays
2. Create a new negative set(does not allows duplicate) which is possible sums of negative arrays ex -
[-1,-2,-3] = [-1,-2,-3, {-1-2=3},{-1,-3=-4},{-2,-3=-5},{-6}] = [-1,-2,-3,-4,-5,-6]
So the set looked like
Key:Value
"1" =-1
"2" = -2
...
"2:3"=-5
"1:2:3"=-6
Here
"N6" = -6
3. For this new set of negative array find combination in positive
array which matches any of the 6 negative arrays.
Same as above say positive numbers are 3 and 4
So the set would look like
"3"=3
"4"=4
"3:4"=7
Now simple compare the two sets and see which of these are equal
So for example Negative Set "1:3" = Positive Set "4"
and hence use Stringtokenizer to get the numbers from set key {-1,-3,4}