How to find the number of unique triplets summing to number K?

45 Views Asked by At

Coming from Stack Overflow forum because someone told me to ask here for rigorous proofs: https://stackoverflow.com/questions/60261555/three-sums-problem-space-complexity-why-is-it-on

For a given sequence of N distinct integers and a target sum K. For example:

$-8, -6, 1, 2, 3, 5, 6, 12$

$K = 0$

We want to find out the number of unique triplets summing to the target sum. In this example, the number of permutations will be 3:

$(-8, 2, 6), (-8, 3, 5), (-6, 1, 5)$

So from here, I can see the permutation is definitely less than ${N\choose 3}$, since all numbers are distinct. How do I mathematically find the number of unique triplets summing to a given number?