The complexity depends on the sorting algorithm you are using. I think the above answer is correct.
Anonymous
Jul 11, 2012
Sorry, the solution above is not correct. for example, if you have a set [-7, -5, -4, 0, 1, 2, 3, 4], the algorithm is not right. Am I missing anything?
Anonymous
Jul 23, 2012
cant we generate permutations of three numbers and use it to get the answer?
1
Anonymous
Jul 11, 2012
It's correct.
When i=7 (assume that index starting from 1) and left = 0, right = 8 we get right answer -7 + 3 + 4
Pseudo algo:
For each i = 1..length do
left = 0, right = length
While lefti do
if values[left] + values[i] + values[right] == 0
>> Got solution here
if values[left] + values[i] + values[right] > 0
# means that right border should be moved
right--
else
# moving left border
left++
Anonymous
Jun 13, 2012
Sort. Fix one element i of array and move to it from left and right of array depending on value of values[left]+values[i]+values[right]
Complexity O(N^2)