Given two integers X and Y, and two values cost1 and cost2, the task is to convert the given two numbers equal to zero at minimal cost by performing the following two types of operations:
- Increase or decrease any one of them by 1 at cost1.
- Increase or decrease both of them by 1 at cost2.
Input: X = 1, Y = 3, cost1 = 391, cost2 = 555
Reduce Y to 1 using the first operation twice and convert both X and Y from 1 to 0 using the second operation.
Hence, the total cost = 391 * 2 + 555 = 1337.
Input: X = 12, Y = 7, cost1 = 12, cost2 = 7
Reduce X to 7 using first operation and then convert both X and Y to 0 using the second operation.
Hence, the total cost = 12 * 5 + 7 * 7 = 109
The most optimal way to solve the problem is:
- Reduce the maximum of X and Y to the minimum by using first operation. This increases the cost by abs(X – Y) * cost1.
- Then, reduce both X and Y to 0 using the second operation. This increase the cost by minimum of (X, Y) * cost2.
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 cost to convert a given matrix to another by flipping columns and reordering rows
- Minimize cost to Swap two given Arrays
- Minimize the cost of selecting two numbers whose product is X
- Minimize the non-zero elements in the Array by given operation
- Minimize cost to empty a given string by removing characters alphabetically
- Minimum Cost to make all array elements equal using given operations
- Minimum cost to reach a point N from 0 with two different operations allowed
- Minimize the cost to split a number
- Minimize the cost of buying the Objects
- Minimize the cost of partitioning an array into K groups
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Check whether product of integers from a to b is positive , negative or zero
- Find N distinct integers with zero sum
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Minimize the value of N by applying the given operations
- Check if at least half array is reducible to zero by performing some operations
- Number from a range [L, R] having Kth minimum cost of conversion to 1 by given operations
- Minimize operations required to obtain N
- Convert a number m to n using minimum number of given operations
- Convert N to M with given operations using dynamic programming
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.