Minimize product of two scores possible by at most M reductions | C ++ program for the above approach ; 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 ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minProductUtil ( int R1 , int B1 , int R2 , int B2 , int M ) { int x = min ( R1 - B1 , M ) ; M -= x ; R1 -= x ; if ( M > 0 ) { int y = min ( R2 - B2 , M ) ; M -= y ; R2 -= y ; } return R1 * R2 ; } int minProduct ( int R1 , int B1 , int R2 , int B2 , int M ) { int res1 = minProductUtil ( R1 , B1 , R2 , B2 , M ) ; int res2 = minProductUtil ( R2 , B2 , R1 , B1 , M ) ; return min ( res1 , res2 ) ; } int main ( ) { int R1 = 21 , B1 = 10 , R2 = 13 , B2 = 11 , M = 3 ; cout << ( minProduct ( R1 , B1 , R2 , B2 , M ) ) ; return 0 ; }