# Minimize cost to convert given two integers to zero using given operations

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**.

**Examples: **

Input:X = 1, Y = 3, cost1 = 391, cost2 = 555

Output:1337

Explanation:

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

Output:4

Explanation:

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

**Approach: **

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:

## C++

`// C++ implementation to find the minimum ` `// cost to make the two integers equal ` `// to zero using given operations ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find out the minimum cost to ` `// make two number X and Y equal to zero ` `int` `makeZero(` `int` `x, ` `int` `y, ` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// If x is greater than y then swap ` ` ` `if` `(x > y) ` ` ` `x = y, ` ` ` `y = x; ` ` ` ` ` `// Cost of making y equal to x ` ` ` `int` `tot_cost = (y - x) * a; ` ` ` ` ` `// Cost if we choose 1st operation ` ` ` `int` `cost1 = 2 * x * a; ` ` ` ` ` `// Cost if we choose 2nd operation ` ` ` `int` `cost2 = x * b; ` ` ` ` ` `// Total cost ` ` ` `tot_cost += min(cost1, cost2); ` ` ` ` ` `cout << tot_cost; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `X = 1, Y = 3; ` ` ` `int` `cost1 = 391, cost2 = 555; ` ` ` ` ` `makeZero(X, Y, cost1, cost2); ` `} ` ` ` `// This code is contributed by coder001 ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the minimum ` `// cost to make the two integers equal ` `// to zero using given operations ` `import` `java.util.*; ` `class` `GFG{ ` ` ` `// Function to find out the minimum cost to ` `// make two number X and Y equal to zero ` `static` `void` `makeZero(` `int` `x, ` `int` `y, ` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// If x is greater than y then swap ` ` ` `if` `(x > y) ` ` ` `{ ` ` ` `int` `temp = x; ` ` ` `x = y; ` ` ` `y = temp; ` ` ` `} ` ` ` ` ` `// Cost of making y equal to x ` ` ` `int` `tot_cost = (y - x) * a; ` ` ` ` ` `// Cost if we choose 1st operation ` ` ` `int` `cost1 = ` `2` `* x * a; ` ` ` ` ` `// Cost if we choose 2nd operation ` ` ` `int` `cost2 = x * b; ` ` ` ` ` `// Total cost ` ` ` `tot_cost += Math.min(cost1, cost2); ` ` ` ` ` `System.out.print(tot_cost); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `X = ` `1` `, Y = ` `3` `; ` ` ` `int` `cost1 = ` `391` `, cost2 = ` `555` `; ` ` ` ` ` `makeZero(X, Y, cost1, cost2); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# minimum cost to make the two integers ` `# equal to zero using given operations ` ` ` ` ` `# Function to find out the minimum cost to make ` `# two number X and Y equal to zero ` `def` `makeZero(x, y, a, b): ` ` ` ` ` `# If x is greater than y then swap ` ` ` `if` `(x > y): ` ` ` `x, y ` `=` `y, x ` ` ` ` ` `# Cost of making y equal to x ` ` ` `tot_cost ` `=` `(y ` `-` `x) ` `*` `a ` ` ` ` ` `# Cost if we choose 1st operation ` ` ` `cost1 ` `=` `2` `*` `x ` `*` `a ` ` ` ` ` `# Cost if we choose 2nd operation ` ` ` `cost2 ` `=` `x ` `*` `b ` ` ` ` ` `# Total cost ` ` ` `tot_cost` `+` `=` `min` `(cost1, cost2) ` ` ` ` ` `print` `(tot_cost) ` ` ` ` ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `X, Y ` `=` `1` `, ` `3` ` ` ` ` `cost1, cost2 ` `=` `391` `, ` `555` ` ` ` ` `makeZero(X, Y, cost1, cost2) ` ` ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the minimum ` `// cost to make the two integers equal ` `// to zero using given operations ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find out the minimum cost to ` `// make two number X and Y equal to zero ` `static` `void` `makeZero(` `int` `x, ` `int` `y, ` `int` `a, ` `int` `b) ` `{ ` ` ` ` ` `// If x is greater than y then swap ` ` ` `if` `(x > y) ` ` ` `{ ` ` ` `int` `temp = x; ` ` ` `x = y; ` ` ` `y = temp; ` ` ` `} ` ` ` ` ` `// Cost of making y equal to x ` ` ` `int` `tot_cost = (y - x) * a; ` ` ` ` ` `// Cost if we choose 1st operation ` ` ` `int` `cost1 = 2 * x * a; ` ` ` ` ` `// Cost if we choose 2nd operation ` ` ` `int` `cost2 = x * b; ` ` ` ` ` `// Total cost ` ` ` `tot_cost += Math.Min(cost1, cost2); ` ` ` ` ` `Console.Write(tot_cost); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `X = 1, Y = 3; ` ` ` `int` `cost1 = 391, cost2 = 555; ` ` ` ` ` `makeZero(X, Y, cost1, cost2); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

1337

**Time Complexity:** *O(1)*

**Auxilary Space: ***O(1)*

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Minimum cost to convert str1 to str2 with the given operations
- Minimize the cost of buying the Objects
- Minimize the cost to split a number
- Minimize Cost with Replacement with other allowed
- Minimize the cost of selecting two numbers whose product is X
- Minimize the cost of partitioning an array into K groups
- Minimize the cost to make all the adjacent elements distinct in an Array
- Minimize the value of N by applying the given operations
- Minimum cost to reach a point N from 0 with two different operations allowed
- Minimum Cost to make all array elements equal using given operations
- Convert N to M with given operations using dynamic programming
- Minimum number operations required to convert n to m | Set-2
- Minimum prime number operations to convert A to B
- Minimum operations required to convert X to Y by multiplying X with the given co-primes
- Minimum number of operations to convert a given sequence into a Geometric Progression
- Minimum number of operations to convert array A to array B by adding an integer into a subarray
- Convert a number m to n using minimum number of given operations
- Median in a stream of integers (running integers)
- Minimize the sum of digits of A and B such that A + B = N
- Minimize the sum of the array according the given condition

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.