Ok thank you I will try that and see how to optimize the calculation if N 
is becoming big.



Le mercredi 15 avril 2020 22:37:26 UTC+2, porker2008 a écrit :
>
> The problem with your code is function *ncr()*
>
> It can easily go overflow when *n* is too big.
>
> Here is one fix for *cnr()* and it allows you to pass test 2 (still 
> failing test 3 because your solution demands too much memory and time to 
> finish)
>
> *static int ncr(int n,int r) {*
> *    int result = 1;*
> *    r = Math.min(r, n - r);*
> *    for (int i = 1; i <= r; i++) {*
> *        result = result * (n - i + 1) / i;*
> *        // Once result is bigger than 1e9*
> *        // there is no need to calculate its actual value*
> *        if (result > 1000000000) {*
> *            return 1000000000;*
> *        }*
> *    }*
> *    return result;*
> *}*
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-code+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/fc4c977e-0215-4fbc-a672-16f5bc4b8585%40googlegroups.com.

Reply via email to