Description:
Given an array consists of n positive integers, find the largest C which makes C=A+B. A,B,C are all in the given array.
Example:
1 1 1 4 5 5 6 6 10 9 C=10
1 3 6 C=-1, This means the largest C does not exist
I’m looking for an algorithm which is less than O(n^3), Can anyone give me some directions?
Sort the list. Iterate over C from largest to smallest, and over B from smallest to the value of C. So far, it’s O(n^2). For each (C, B) pair, calculate A, and you just need to find if it’s in the array. You can use a binary search for an overall time of O(n^2 log n).