I have the following algorithm to find all triples
for (int i = 0; i < N; i++)
for (int j = i+1; j < N; j++)
for (int k = j+1; k < N; k++)
if (a[i] + a[j] + a[k] == 0)
{ cnt++; }
I now that I have triple loop and I check all triples. How to show that the number of different triples that can be chosen from N items is precisely N*(N-1)*(N-2)/6 ?
If we have two loops
for (int i = 0; i < N; i++)
for (int j = i+1; j < N; j++)
...
when i = 0 we go to the second loop N-1 times
i = 1 => N-2 times
...
i = N-1 => 1 time
i = N => 0 time
So 0 + 1 + 2 + 3 + ... + N-2 + N-1 = ((0 + N-1)/2)*N = N*N - N/2
But how to do the same proof with triples?




