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.
Input: X = 5, Y= 6, N = 4
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
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.
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:
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.
- Minimize increments or decrements required to make sum and product of array elements non-zero
- Minimize increments or decrements by 2 to convert given value to a perfect square
- Minimize subarray increments/decrements required to reduce all array elements to 0
- Maximize absolute difference between X and Y by at most N decrements
- Minimize the cost of selecting two numbers whose product is X
- Minimize sum of K positive integers with given LCM
- Minimum decrements to make integer A divisible by integer B
- Minimum decrements required such that sum of all adjacent pairs in an Array does not exceed K
- Find K that requires minimum increments or decrements of array elements to obtain a sequence of increasing powers of K
- Minimum Decrements on Subarrays required to reduce all Array elements to zero
- Count decrements to nearest smaller element required to make all array elements equal
- Nth positive number whose absolute difference of adjacent digits is at most 1
- Minimize the sum of product of two arrays with permutations allowed
- Minimize product of first N - 1 natural numbers by swapping same positioned bits of pairs
- Minimize the number by changing at most K digits
- Minimize the maximum difference of adjacent elements after at most K insertions
- Minimize difference between maximum and minimum of Array by at most K replacements
- Minimize Sum of an Array by at most K reductions
- Minimum number to be added to minimize LCM of two given numbers
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.