- Given 4 arrays that can contain positive and negative numbers.
- find all possible sets with one number from each array (ie each set will contain 4 numbers ) such that sum of 4 numbers is zero.
Given 4 arrays that can contain positive and negative numbers. find all possible sets
Share
Adrian just hasn’t thought far enough 🙂
Loop through array 1 and 2 and add all sums to a map.
Now from the other 2 arrays find all combinations which add up to a number in the map which you got from array 1 and 2. It’s pretty straight forward. Let me know if you need some pseudocode.
O(n^2) runtime