If arrays contain integers with a limited size (i.e., in the range from -u to u), you can solve this O(n+ulogu) times using the fast Fourier transform to convolve the histograms of each collection together.
For example, the set a=[-1,2,2,2,2,3] will be represented by a histogram with the values:
ha[-1] = 1 ha[2] = 4 ha[3] = 1
After collapsing all the histograms together with the FFT, the resulting histogram will contain entries in which the value for each bin tells you the number of ways to combine numbers to get each possible total. To find the answer to your question for a total of 0, all you have to do is read the histogram value for bin 0.
Peter de Rivaz
source share