How to calculate nCr?

Pascal Triangle is triangle array of the binomial coefficients that arises in probability theory, combinatorics, and algebra

1
1 1
1 2 1
1 3 3 1
...

Let x row and y column. The element E[x, y] = E[x - 1, y - 1] + E[x - 1, y]. *Then E[n + 1, r + 1] is also the representation of nCr.

You can calculate nCr by tracing the value at column r + 1.

vector<int> v(y+1, 0)
v[0] = 1; // value at 0 index is always 1.

for(int i = 1; i <= x; i++){
    for(int j = y; int j > 0; j--){
         v[j] = ((v[j] % mod) + (v[j-1] % mod)) % mod
    }
}

return v[y]

discussion