Minimize product of two scores possible by at most M reductions
Last Updated :
17 Aug, 2021
Given two integers R1, R2 denoting runs scored by two players, and B1, B2 denoting balls faced by them respectively, the task is to find the minimum value of R1 * R2 that can be obtained such that R1 and R2 can be reduced by M runs satisfying the conditions R1 ? B1 and R2 ? B2.
Examples:
Input: R1 = 21, B1 = 10, R2 = 13, B2 = 11, M = 3
Output: 220
Explanation: Minimum product that can be obtained is by decreasing R1 by 1 and R2 by 2, i.e. (21 – 1) x (13 – 2) = 220.
Input: R1 = 7, B1 = 6, R2 = 9, B1 = 9, M = 4
Output: 54
Approach: The minimum product can be obtained by reducing the numbers completely to their limits. Reduce R1 to its limit B1 and then reduce R2 as less as possible (not exceeding M). Similarly, reduce R2 to at most B2 and then reduce R2 as less as possible (not exceeding M). The minimum product obtained in the two cases is the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
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;
}
|
Java
import java.io.*;
class GFG{
static int minProductUtil( int R1, int B1, int R2,
int B2, int M)
{
int x = Math.min(R1-B1, M);
M -= x;
R1 -= x;
if (M > 0 )
{
int y = Math.min(R2 - B2, M);
M -= y;
R2 -= y;
}
return R1 * R2;
}
static 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 Math.min(res1, res2);
}
public static void main (String[] args)
{
int R1 = 21 , B1 = 10 , R2 = 13 , B2 = 11 , M = 3 ;
System.out.print((minProduct(R1, B1, R2, B2, M)));
}
}
|
Python3
def minProductUtil(R1, B1, R2, B2, M):
x = min (R1 - B1, M)
M - = x
R1 - = x
if M > 0 :
y = min (R2 - B2, M)
M - = y
R2 - = y
return R1 * R2
def minProduct(R1, B1, R2, B2, M):
res1 = minProductUtil(R1, B1, R2, B2, M)
res2 = minProductUtil(R2, B2, R1, B1, M)
return min (res1, res2)
if __name__ = = '__main__' :
R1 = 21 ; B1 = 10 ; R2 = 13 ; B2 = 11 ; M = 3
print (minProduct(R1, B1, R2, B2, M))
|
C#
using System;
class GFG{
static int minProductUtil( int R1, int B1, int R2,
int B2, int M)
{
int x = Math.Min(R1-B1, M);
M -= x;
R1 -= x;
if (M > 0)
{
int y = Math.Min(R2 - B2, M);
M -= y;
R2 -= y;
}
return R1 * R2;
}
static 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 Math.Min(res1, res2);
}
public static void Main (String[] args)
{
int R1 = 21, B1 = 10, R2 = 13, B2 = 11, M = 3;
Console.Write((minProduct(R1, B1, R2, B2, M)));
}
}
|
Javascript
<script>
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)));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...