We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Also, for those of us who don't quickly and intuitively do algebra. The generic binomial formula is: n! / k! * (n-k)!
If you use the generic formula, you're gonna get integer overflow, even with u128 integers (35!).
But because the k is fixed:
n! / (2! * (n-2)!)
(1 / 2!) * (n! / (n-2)!)
And by definition of factorial n! => (n-2)! * (n-1) * n, so
(1/2) * ( ((n-2)! * (n-1) * n) / (n-2)! )
And the (n-2)! factorial cancels out, so
we have:
(n * (n-1)) / 2
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Anagrams
You are viewing a single comment's thread. Return to all comments →
Also, for those of us who don't quickly and intuitively do algebra. The generic binomial formula is: n! / k! * (n-k)! If you use the generic formula, you're gonna get integer overflow, even with u128 integers (35!). But because the k is fixed:
n! / (2! * (n-2)!)
(1 / 2!) * (n! / (n-2)!)
And by definition of factorial n! => (n-2)! * (n-1) * n, so
(1/2) * ( ((n-2)! * (n-1) * n) / (n-2)! )
And the (n-2)! factorial cancels out, so we have:
(n * (n-1)) / 2