Determine whether 4 elements in an array sum to 0 in O(n^2) time.
Sigiloso
Yeah, it's impossible to do it O(n^2) worst case, but very possible O(n^2) average case. Here: 1. Hash the sum of all pairs -> O(n^2) average // be sure to store the indices to avoid double counting. 2. Now just go through the original list, and for each pair of elements, check if a non intersecting -sum is found in the hash table from part 1. -> O(n^2) average. In case it is not obvious why 2 is O(n^2) despite the fact that we are "searching" for Non-Intersecting sums, you should convince yourself that even if all pairs sum to a specific number, you will only need to check a constant number of elements. (hint: grid)