Product of all Subsequences of size K except the minimum and maximum Elements | Javascript program to find product of all Subsequences of size K except the minimum and maximum Elements ; 2D array to store value of combinations nCr ; Function to pre - calculate value of all combinations nCr ; Function to calculate product of all subsequences except the minimum and maximum elements ; Sorting array so that it becomes easy to calculate the number of times an element will come in first or last place ; An element will occur ' powa ' times in total of which ' powla ' times it will be last element and ' powfa ' times it will be first element ; In total it will come powe = powa - powla - powfa times ; Multiplying a [ i ] powe times using Fermat Little Theorem under MODulo MOD for fast exponentiation ; pre - calculation of all combinations
let MOD = 1000000007 ; let max = 101 ; let C = new Array ( max ) ; for ( let i = 0 ; i < max ; i ++ ) { C [ i ] = new Array ( max ) ; for ( let j = 0 ; j < max ; j ++ ) { C [ i ] [ j ] = 0 ; } } function power ( x , y ) { let res = 1 ; x = x % MOD ; while ( y > 0 ) { if ( y % 2 == 1 ) { res = ( res * x ) % MOD ; } y = y >> 1 ; x = ( x * x ) % MOD ; } return res % MOD ; } function combi ( n , k ) { let i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] % MOD + C [ i - 1 ] [ j ] % MOD ) % MOD ; } } } function product ( a , n , k ) { let ans = 1 ; a . sort ( function ( a , b ) { return a - b } ) ; let powa = C [ n - 1 ] [ k - 1 ] ; for ( let i = 0 ; i < n ; i ++ ) { let powla = C [ i ] [ k - 1 ] ; let powfa = C [ n - i - 1 ] [ k - 1 ] ; let powe = ( ( powa % MOD ) - ( powla + powfa ) % MOD + MOD ) % MOD ; let mul = power ( a [ i ] , powe ) % MOD ; ans = ( ( ans % MOD ) * ( mul % MOD ) ) % MOD ; } return ans % MOD ; } combi ( 100 , 100 ) ; let arr = [ 1 , 2 , 3 , 4 ] ; let n = arr . length ; let k = 3 ; let ans = product ( arr , n , k ) ; document . write ( ans ) ;