tags:

views:

129

answers:

2

Hi,

I have been struggling with this questions for sometime now. The question goes like this:-

We have n^2 numbers. We need to find out if there exists a triplet a,b,c such that a+b+c = 0. For a more generic case, a+b+c = k. (k is given)

There exists a solution with O(n^2log(n)) complexity.

Any help would be greatly appreciated.

thanks

A: 

See if this helps.

KMan
+2  A: 

To get this in O(n²logn), you'd have to sort the numbers. Find all combinations of 2 numbers, and do a binary search to find the third.

The upper bound is much higher for the general version of the problem.

Anurag
the number of combinations would be O(n^4). we are looking for an optimized solution.
Pigol
so basically, to rephrase, you're saying there are n items and there's a solution in O(nlog√n)?
Anurag
@Anurag, come on, log√n equals ½log n
Pavel Shved
fair enough Pavel, so it's in O(nlogn).. and here I was rejoicing at the choice of unicode characters :)
Anurag