 <p>I suggest kind of <em>meet in the middle</em> algorithm:</p> <pre><code> A[i] + B[j] + C[k] + D[l] = 0 </code></pre> <p>actually means to find out <code>A[i] + B[j]</code> and <code>C[k] + D[l]</code> such that</p> <pre><code> (A[i] + B[j]) == (-C[k] - D[l]) </code></pre> <p>We can put all possible <code>A[i] + B[j]</code> sums into a <em>dictionary</em> and then, in the loop over <code>-C[k] - D[l]</code>, try to look up in this dictionary. You can implement it like this:</p> <pre><code> private static int FourSumCount(int[] A, int[] B, int[] C, int[] D) { // left part: all A[i] + B[j] combinations Dictionary&lt;int, int&gt; left = new Dictionary&lt;int, int&gt;(); // loop over A[i] + B[j] combinations foreach (var a in A) foreach (var b in B) { int k = a + b; int v; if (left.TryGetValue(k, out v)) left[k] = v + 1; // we have such a combination (v of them) else left.Add(k, 1); // we don't have such a combination } int result = 0; // loop over -C[k] - D[l] combinations foreach (var c in C) foreach (var d in D) { int v; if (left.TryGetValue(-c - d, out v)) result += v; } return result; } </code></pre> <p>As you can see, we have <code>O(|A| * |B| + |C| * |D|)</code> complexity; in case <code>A</code>, <code>B</code>, <code>C</code> and <code>D</code> arrays have the approximately equal sizes <code>N</code> the complexity is <code>O(N**2)</code>.</p>