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 zeroint 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 |
Java
// Java implementation to find the minimum// cost to make the two integers equal// to zero using given operationsimport java.util.*;class GFG{     // Function to find out the minimum cost to // make two number X and Y equal to zerostatic 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 |
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 zerodef 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)    |
C#
// C# implementation to find the minimum// cost to make the two integers equal// to zero using given operationsusing System;Â
class GFG{     // Function to find out the minimum cost to // make two number X and Y equal to zerostatic 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 |
Javascript
<script>Â
// Javascript 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 zerofunction makeZero(x, y, a, b){         // If x is greater than y then swap    if (x > y)    {        let temp = x;        x = y;        y = temp;    }Â
    // Cost of making y equal to x     let tot_cost = (y - x) * a;Â
    // Cost if we choose 1st operation    let cost1 = 2 * x * a;Â
    // Cost if we choose 2nd operation    let cost2 = x * b;Â
    // Total cost    tot_cost += Math.min(cost1, cost2);Â
    document.write(tot_cost);}Â
// Driver codelet X = 1, Y = 3;let cost1 = 391, cost2 = 555;Â
makeZero(X, Y, cost1, cost2);Â
// This code is contributed by rameshtravel07Â Â Â
</script> |
1337
Â
Time Complexity: O(1)Â
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



