Probability such that two subset contains same number of elements | Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; Initialize result ; If y is odd , multiply x with result ; y must be even now y = y / 2 ; Change x to x ^ 2 ; Recursive function to return gcd of a and b ; Function to find probability ; Calculate total possible ways and favourable ways . ; Divide by gcd such that they become relatively coprime ; Driver code
function binomialCoeff ( n , k ) { let res = 1 ; if ( k > n - k ) k = n - k ; for ( let i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res = parseInt ( res / ( i + 1 ) ) ; } return res ; } function power ( x , y ) { let res = 1 ; while ( y > 0 ) { if ( y & 1 ) res = res * x ; y = y >> 1 ; x = x * x ; } return res ; } function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function FindProbability ( n ) { let up = binomialCoeff ( 2 * n , n ) ; let down = power ( 2 , 2 * n ) ; let g = gcd ( up , down ) ; up = parseInt ( up / g ) , down = parseInt ( down / g ) ; document . write ( up + " " + down + " " ) ; } let N = 8 ; FindProbability ( N ) ;