I have a piece code which i am submitting and it always give wrong answer. I had tough time figuring out the logic and then I saw someone’s else’s code logic and tried to replicate the same, but still got the same issue. When I submit the same C++ version of the code it works fine, but with my code it gives me wrong answer, I am not sure, if I am missing something here. Can you guys help?
#include <stdio.h>
long long int factorial(long long int n, long long int r);
int main(int argc, const char * argv[]) {
int t;
scanf("%d", &t);
while (t > 0)
{
long long int n, k;
scanf("%lld%lld", &n, &k);
long long int result = factorial(n-1, n-k);
printf("%lld", result);
t--;
}
return 0;
}
long long int factorial(long long int n, long long int r)
{
long long int res = 1, i;
if (r > n / 2)
r = n - r;
for (i = 0; i < r; i++)
{
res = res * n;
res /= i + 1;
n--;
}
return res;
}