O(n^2) algorithm using sorting as preprocessing, and using routine to check sum of pairs of elements equals a[i] for all i.
Is there a better solution?
1
Sigiloso
15 de out. de 2012
not the best solution
private static void ThreeNumberZero(int[] p)
{
for (int i = 0; i < p.Length - 2; i++)
{
for (int j = i + 1; j < p.Length - 1; j++)
{
for (int k = j + 1; k < p.Length; k++)
{
if(p[i] + p[j] + p[k] == 0)
{
Console.WriteLine(p[i] + " " + p[j] + " " + p[k]);
}
}
}
}
}
1
Sigiloso
9 de dez. de 2011
There are detailed analysis and solutions on the blog,
http://codercareer.blogspot.com/2011/10/no-09-numbers-with-given-sum.html
please check the second problem on the web page.