Minimize product of two scores possible by at most M reductions | Utility function to find the minimum product of R1 and R2 possible ; Reaching to its limit ; If M is remaining ; Function to find the minimum product of R1 and R2 ; Case 1 - R1 reduces first ; Case 2 - R2 reduces first ; Given Input ; Function Call
function minProductUtil ( R1 , B1 , R2 , B2 , M ) { let x = Math . min ( R1 - B1 , M ) ; M -= x ; R1 -= x ; if ( M > 0 ) { let y = Math . min ( R2 - B2 , M ) ; M -= y ; R2 -= y ; } return R1 * R2 ; } function minProduct ( R1 , B1 , R2 , B2 , M ) { let res1 = minProductUtil ( R1 , B1 , R2 , B2 , M ) ; let res2 = minProductUtil ( R2 , B2 , R1 , B1 , M ) ; return Math . min ( res1 , res2 ) ; } let R1 = 21 , B1 = 10 , R2 = 13 , B2 = 11 , M = 3 ; document . write ( ( minProduct ( R1 , B1 , R2 , B2 , M ) ) ) ;