Scroll array 1 and add all the elements to the map.
Now, from the remaining 3 arrays, they find all combinations that add to the number on the map that you got from array 1. This is pretty straight forward. Let me know if you need pseudo code. O (n ^ 3) runtime
The best solution is to group 2 by 2 arrays and do the sum on them. You can generalize it to n arrays (where n is equal). You are building a tree structure where each node is an array. Leaves are the initial given arrays, then one level up, you have the addition of 2 arrays (from leaves), etc. nlogn , where n is the average size of the arrays. (for each element of S @position i [in arrays] you create a tree)
EDIT: Just a note (for historical reasons)
I remember I had the same question. What I did then was to use this binary tree method, but I calculated the combinations at each stage of the tree. I took 2 arrays and combined them into one larger array of size n ^ 2. Then, in the next step, the size of the new array was n ^ 4, etc. When I have 2 arrays left, I matched them. Then just check if the elements are in another array on the map.
source share