text: Ways to multiply n elements with an associative operation | Java code to find number of ways to multiply n elements with an associative operation ; Function to find the required factorial ; Function to find nCr ; function to find the number of ways ; Driver Code
code: import java . io . * ; class GFG { static int fact ( int n ) { if ( n == 0 n == 1 ) return 1 ; int ans = 1 ; for ( int i = 1 ; i <= n ; i ++ ) ans = ans * i ; return ans ; } static int nCr ( int n , int r ) { int Nr = n , Dr = 1 , ans = 1 ; for ( int i = 1 ; i <= r ; i ++ ) { ans = ( ans * Nr ) / ( Dr ) ; Nr -- ; Dr ++ ; } return ans ; } static int solve ( int n ) { int N = 2 * n - 2 ; int R = n - 1 ; return nCr ( N , R ) * fact ( n - 1 ) ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( solve ( n ) ) ; } }