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:

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to minimize
// the product of two numbers
int minProd(int X, int Y,
            int N)
{
    if (X <= Y) {
 
        if (N < X)
 
            // Reducing X, N times,
            // minimizes the product
            return (X - N) * Y;
        else {
 
            // Reduce X to 1 and reduce
            // remaining N from Y
            return max(Y - (N - X + 1), 1);
        }
    }
 
    if (Y >= N)
 
        // Reducing Y, N times,
        // minimizes the product
        return (Y - N) * X;
 
    // Reduce Y to 1 and reduce
    // remaining N from X
    return max(X - (N - Y + 1), 1);
    ;
}
 
// Driver Code
int main()
{
    int X = 47, Y = 42, N = 167;
    cout << minProd(X, Y, N);
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to implement
// the above approach
import java.io.*;
 
class GFG{
 
// Function to minimize
// the product of two numbers
static int minProd(int X, int Y,
                   int N)
{
    if (X <= Y)
    {
        if (N < X)
   
            // Reducing X, N times,
            // minimizes the product
            return (X - N) * Y;
        else
        {
             
            // Reduce X to 1 and reduce
            // remaining N from Y
            return Math.max(Y - (N - X + 1), 1);
        }
    }
   
    if (Y >= N)
   
        // Reducing Y, N times,
        // minimizes the product
        return (Y - N) * X;
   
    // Reduce Y to 1 and reduce
    // remaining N from X
    return Math.max(X - (N - Y + 1), 1);
}
   
// Driver Code
public static void main (String[] args)
{
    int X = 47, Y = 42, N = 167;
     
    System.out.println(minProd(X, Y, N));
}
}
 
// This code is contributed by code_hunt
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
 
# Function to minimize
# the product of two numbers
def minProd(X, Y, N):
    if (X <= Y):
 
        if (N < X):
 
            # Reducing X, N times,
            # minimizes the product
            return (X - N) * Y
        else:
 
            # Reduce X to 1 and reduce
            # remaining N from Y
            return max(Y - (N - X + 1), 1)
 
    if (Y >= N):
 
        # Reducing Y, N times,
        # minimizes the product
        return (Y - N) * X
 
    # Reduce Y to 1 and reduce
    # remaining N from X
    return max(X - (N - Y + 1), 1)
 
# Driver Code
if __name__ == "__main__":
    X = 47
    Y = 42
    N = 167
    print (minProd(X, Y, N))
 
# This code is contributed by Chitranayal
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to implement
// the above approach
using System;
class GFG{
 
// Function to minimize
// the product of two numbers
static int minProd(int X,
                   int Y, int N)
{
  if (X <= Y)
  {
    if (N < X)
 
      // Reducing X, N times,
      // minimizes the product
      return (X - N) * Y;
    else
    {
      // Reduce X to 1 and reduce
      // remaining N from Y
      return Math.Max(Y - (N -
                      X + 1), 1);
    }
  }
 
  if (Y >= N)
 
    // Reducing Y, N times,
    // minimizes the product
    return (Y - N) * X;
 
  // Reduce Y to 1 and reduce
  // remaining N from X
  return Math.Max(X - (N -
                  Y + 1), 1);
}
   
// Driver Code
public static void Main(String[] args)
{
  int X = 47, Y = 42, N = 167;
  Console.WriteLine(minProd(X, Y, N));
}
}
 
// This code is contributed by Rajput-Ji
chevron_right

Output: 
1









 

Time Complexity: O(1)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : code_hunt, chitranayal, Rajput-Ji

Article Tags :
Practice Tags :