Minimize positive product of two given numbers by at most N decrements
Given three integers X, Y, and N, the task is to find the minimum possible positive product of X and Y that can be obtained by decreasing either the value of X or Y by 1 at most N times.
Examples:
Input: X = 5, Y= 6, N = 4
Output: 6
Explanation:
Decrease the value of X by 4, X = 5 – 4 = 1 and Y = 6.
Therefore, the minimized product = X * Y = 1 * 6 = 6
Input: X = 49, Y = 4256, N = 10
Output: 165984
Approach: The given problem can be solved based on the following observations:
If X ? Y: Reducing X minimizes the product.
If Y ? X: Reducing Y minimizes the product.
Mathematical Proof:
If (X – 2) * Y < (X – 1) * (Y – 1)
=> X * Y – 2 * Y < X * Y – X – Y + 1
=> – 2 × Y < -X – Y + 1
=> Y > X – 1
Follow the steps below to solve the problem:
- If X ? Y: Follow the steps below:
- If N < X: Print Y * (X – N) as the answer as reducing X minimizes the product.
- Otherwise, reduce X to 1 and reduce the remaining N from Y to minimize the product. Therefore, print Y – max(1, N – X + 1)) as the required minimized product.
- Otherwise, if N < Y, print X * (Y – N) as the minimized product. If N ? Y, reduce Y to 1 and print max(X – (N – Y + 1), 1) as the minimized product.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minProd( int X, int Y,
int N)
{
if (X <= Y) {
if (N < X)
return (X - N) * Y;
else {
return max(Y - (N - X + 1), 1);
}
}
if (Y >= N)
return (Y - N) * X;
return max(X - (N - Y + 1), 1);
;
}
int main()
{
int X = 47, Y = 42, N = 167;
cout << minProd(X, Y, N);
}
|
Java
import java.io.*;
class GFG{
static int minProd( int X, int Y,
int N)
{
if (X <= Y)
{
if (N < X)
return (X - N) * Y;
else
{
return Math.max(Y - (N - X + 1 ), 1 );
}
}
if (Y >= N)
return (Y - N) * X;
return Math.max(X - (N - Y + 1 ), 1 );
}
public static void main (String[] args)
{
int X = 47 , Y = 42 , N = 167 ;
System.out.println(minProd(X, Y, N));
}
}
|
Python3
def minProd(X, Y, N):
if (X < = Y):
if (N < X):
return (X - N) * Y
else :
return max (Y - (N - X + 1 ), 1 )
if (Y > = N):
return (Y - N) * X
return max (X - (N - Y + 1 ), 1 )
if __name__ = = "__main__" :
X = 47
Y = 42
N = 167
print (minProd(X, Y, N))
|
C#
using System;
class GFG{
static int minProd( int X,
int Y, int N)
{
if (X <= Y)
{
if (N < X)
return (X - N) * Y;
else
{
return Math.Max(Y - (N -
X + 1), 1);
}
}
if (Y >= N)
return (Y - N) * X;
return Math.Max(X - (N -
Y + 1), 1);
}
public static void Main(String[] args)
{
int X = 47, Y = 42, N = 167;
Console.WriteLine(minProd(X, Y, N));
}
}
|
Javascript
<script>
function minProd(X, Y, N)
{
if (X <= Y)
{
if (N < X)
return (X - N) * Y;
else
{
return Math.max(Y - (N - X + 1), 1);
}
}
if (Y >= N)
return (Y - N) * X;
return Math.max(X - (N - Y + 1), 1);
}
let X = 47, Y = 42, N = 167;
document.write(minProd(X, Y, N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
28 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...