I was reading the possible efficient methods for calculating
ncr
when I came across this post.
Which is better way to calculate nCr
The second answer given here, an I'm not able to understand that. The code is:
long long combi(int n,int k)
{
long long ans=1;
k=k>n-k?n-k:k;
int j=1;
for(;j<=k;j++,n--)
{
if(n%j==0)
{
ans*=n/j;
}else
if(ans%j==0)
{
ans=ans/j*n;
}else
{
ans=(ans*n)/j;
}
}
return ans;
}
And what will be the complexity for this? I tried doing it with an example and the answer comes out right but what are those conditions?