Count of groups among N people having only one leader in each group | Javascript program for the above approach ; Function to find 2 ^ x using modular exponentiation ; Base cases ; If B is even ; If B is odd ; Function to count the number of ways to form the group having one leader ; Find 2 ^ ( N - 1 ) using modular exponentiation ; Count total ways ; Print the total ways ; Given N number of peoples ; Function Call
let mod = 1000000007 ; function exponentMod ( A , B ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; let y ; if ( B % 2 == 0 ) { y = exponentMod ( A , B / 2 ) ; y = ( y * y ) % mod ; } else { y = A % mod ; y = ( y * exponentMod ( A , B - 1 ) % mod ) % mod ; } return ( ( y + mod ) % mod ) ; } function countWays ( N ) { let select = exponentMod ( 2 , N - 1 ) ; let ways = ( ( N % mod ) * ( select % mod ) ) ; ways %= mod ; document . write ( ways ) ; } let N = 5 ; countWays ( N ) ;