Find 3 numbers in an array adding up to a given sum S.
Anonymous
This is called the 3SUM problem. Given an array a[n], find three elements a[i] + a[j] + a[k] = S (for distinct values of i, j, and k). The standard solution is O(n^2): sort(a, n); for (int i = 0; i s) k--; else output("Solution found: a[%d] + a[%d] + a[%d] = %g\n", i, j, k, result); } }
Check out your Company Bowl for anonymous work chats.