I donβt know how difficult my proposal will be, but I have an idea :)
What you can do is try to reduce the element from the main array, which is not able to contribute to the solution of the problem, suppose that the elements are -10, 5, 2, -2, 5,7 ,-5, 9,11,19
so you can see that -10,9,11 and 19 are elements that are never useful to make sum 0 in your case
so try removing -10,9,11, and 19 from the main array for this you can do
1) create two sub array from your main array `positive {5,7,2,9,11,19}` and `negative {-10,-2,-5}` 2) remove element from positive array which does not satisfy condition condition -> value should be construct from negative arrays element or sum of its elements ie. 5 = -5 //so keep it //don't consider the sign 7 = (-5 + -2 ) // keep 2 = -2 // keep 9 // cannot be construct using -10,-2,-5 same for all 11 and 19 3) remove element form negative array which does not satisfy condition condition -> value should be construct from positive arrays element or sum of its elements ie -10 // cannot be construct so discard -2 = 2 // keep -5 = 5 // keep
so finally you got an array that contains -2, -5,5,7,2, create all possible auxiliary arrays and check for sum = 0
(Note if your input array contains 0, add all 0 to the final array)
source share