Find the Number of Maximum Product Quadruples | Returns the number of ways to select r objects out of available n choices ; ncr = ( n * ( n - 1 ) * ( n - 2 ) * ... . . ... ( n - r + 1 ) ) / ( r * ( r - 1 ) * ... * 1 ) ; Returns the number of quadruples having maximum product ; stores the frequency of each element ; remaining_choices denotes the remaining elements to select inorder to form quadruple ; Getting an iterator ; If Frequency of element < remaining choices , select all of these elements , else select only the number of elements required ; Decrement remaining_choices acc to the number of the current elements selected ; if the quadruple is formed stop the algorithm ; Driver Code
function NCR ( n , r ) { let numerator = 1 ; let denominator = 1 ; while ( r > 0 ) { numerator *= n ; denominator *= r ; n -- ; r -- ; } return Math . floor ( numerator / denominator ) ; } function findWays ( arr , n ) { let count = new Map ( ) ; if ( n < 4 ) return 0 ; for ( let i = 0 ; i < n ; i ++ ) { count . set ( arr [ i ] , ( count . get ( arr [ i ] ) == null ? 0 : count . get ( arr [ i ] ) + 1 ) ) ; } let remaining_choices = 4 ; let ans = 1 ; for ( let [ key , value ] of count . entries ( ) ) { let number = key ; let frequency = value ; let toSelect = Math . min ( remaining_choices , frequency ) ; ans = ans * NCR ( frequency , toSelect ) ; remaining_choices -= toSelect ; if ( remaining_choices == 0 ) { break ; } } return ans ; } let arr = [ 1 , 2 , 3 , 3 , 3 , 5 ] ; let n = arr . length ; let maxQuadrupleWays = findWays ( arr , n ) ; document . write ( maxQuadrupleWays ) ;