Given an unsorted array of integers, find a 3-element subset that sums to zero
Sigiloso
O(n^2lgn) is easy to come up with, and with a little tweak is O(n^2). O(n^2lgn) solution: vector vals = {1,-2,8,5,9,4,-10,7}; multi_set sorted_vals(vals.begin(), vals.end()); for (auto it = sorted_vals.begin(); it != sorted_vals.end(); it++) for (auto it2 = sorted_vals.begin(); it2 != sorted_vals.end(); it2++) if (it != it2) for (auto it3 = sorted_vals.lower_bound(-*it-*it2), it3_upper = sorted_vals.upper_bound(-*it-*it2); it3 != it3_upper; it3++) if (it3 != it && it3 != it2) { cout << *it << ' ' << *it2 << ' ' << *it3; return 0; }