Input: Set S of numbers Output: output a,b,c such that a+b+c=0 , a,b,c are distinct and in S or no such numbers exit. Idea: Make a < b < c Algorithm: Sort(S) // sort numbers in increasing order. for i=0 to n-3 // indices go from 0 to n-1 in S. The highest index of a can be n-3 { a = S[i] j=i+1 k=n-1 // clever step burning the candle from both sides b = S[j] c = S[k] while (j < k) { if (a+b+c == 0) return (a,b,c) if (a+b+c < 0 ) j = j+1 b = S[j] else k = k-1 c = S[j] } } return (-1) // no solution exists For each possible a (there are O(n) choices, b and c are found in O(n) steps - So the time complexity is O(n^2). Think how will you modify the algorithm if distinctness is omitted. Extra Credit: This problem is known as Knapsack problem (we will study this problem using Dynamic Programming)