3Sum problem explanation I'm working through an Algorithms course online, and they don't explain the below math enough for me to understand.
The code is a simple example for calculating the 3sum problem. The part I don't understand is this:
(N(N - 1)(n - 2))/3!
Where does this come from? I get part of it, 3! is the based on the number of loops. If you were to add a fourth loop, it would be 4!. Same with the numerator, N(N - 1)(N - 2) is to like that because of the offset of the inner arrays. If you added another for statement, it would be N(N - 1)(N - 2)(N - 3). What I don't understand is why? How was this calculated?

 A: The bounds on the loops are such that $i$, $j$, and $k$ are all distinct.  So there are $n$ possibilities for the first number, $n-1$ for the second, and $n-2$ for the third (since it must not be equal to either $i$ or $j$). (There are technically $3!$ ways that we could access the same set of numbers, but since we always have our set sorted (where $i<j<k$) we will only come across one of them.)
A: Since every 3-tuple of distinct indices $i,j,k$ (and only them) will correspond to the "inner loop" line, and there are $\binom{N}{3}$ ways of choosing 3 disticnts elements out of $N$, there will be $3\cdot \binom{N}{3}$ array accesses. Now,
$$ \binom{N}{3} = \frac{N!}{3!(N-3)!} =  \frac{N(N-!)(N-2)}{3!}$$
by definition of the binomial coefficient.
Note that you can also directly compute the number of times the inner loop line is executed as
$$
\sum_{i=0}^{N-1}\sum_{j=i+1}^{N-1}\sum_{k=j+1}^{N-1}1 = \sum_{i=0}^{N-1}\sum_{j=i+1}^{N-1} (N-j-1) = (\cdots)
$$
(it's possible, but more fastidious)
A: As you can see in the loops, we are generating $i$, $j$ and $k$ such as $i<j<k$.
We could do the math and verify directly that it gives $\frac{N(N-1)(N-2)}{3!}$ possibilities, but it is easier to get it differently.
Taking $i$ from $[1,N]$, then $j$ from $[i+1,N]$ then $k$ from $[j+1,N]$ is in fact the same thing as taking 3 different numbers then ordering them and naming them $i$, $j$ and $k$ in order to have $i<j<k$. 
Seeing the problem this way, the question becomes : "How many different triplets can you generate between 1 and N ?".
The answer is now straightforward : it is $\dbinom{N}{3}$, so $\frac{N(N-1)(N-2)}{3!}$.
