Minimize cost to make X and Y equal by given increments
Given two integers X and Y, the task is to make both the integers equal by performing the following operations any number of times:
- Increase X by M times. Cost = M – 1.
- Increase Y by N times. Cost = N – 1.
Examples:
Input: X = 2, Y = 4
Output: 1
Explanation:
Increase X by 2 times. Therefore, X = 2 * 2 = 4. Cost = 1.
Clearly, X = Y. Therefore, total cost = 1.
Input: X = 4, Y = 6
Output: 3
Explanation:
Increase X by 3 times, X = 3 * 4 = 12. Cost = 2.
increase Y by 2 times, Y = 2 * 6 = 12. Cost = 1.
Clearly, X = Y. Therefore, total cost = 2 + 1 = 3.
Naive Approach: Follow the steps below to solve the problem:
- For each value of X, if Y is less than X, then increment Y and update cost.
- If X = Y, then print the total cost.
- If X < Y, then increment X and update cost.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int minimumCost( int x, int y)
{
int costx = 0, dup_x = x;
while ( true ) {
int costy = 0, dup_y = y;
while (dup_y != dup_x
&& dup_y < dup_x) {
dup_y += y;
costy++;
}
if (dup_x == dup_y)
return costx + costy;
else {
dup_x += x;
costx++;
}
}
}
int main()
{
int x = 5, y = 17;
cout << minimumCost(x, y) << endl;
}
|
Java
import java.util.*;
class GFG{
static int minimumCost( int x, int y)
{
int costx = 0 , dup_x = x;
while ( true )
{
int costy = 0 , dup_y = y;
while (dup_y != dup_x
&& dup_y < dup_x)
{
dup_y += y;
costy++;
}
if (dup_x == dup_y)
return costx + costy;
else {
dup_x += x;
costx++;
}
}
}
public static void main(String[] args)
{
int x = 5 , y = 17 ;
System.out.print(minimumCost(x, y) + "\n" );
}
}
|
Python3
def minimumCost(x, y):
costx, dup_x = 0 , x
while ( True ):
costy, dup_y = 0 , y
while (dup_y ! = dup_x and
dup_y < dup_x):
dup_y + = y
costy + = 1
if (dup_x = = dup_y):
return costx + costy
else :
dup_x + = x
costx + = 1
if __name__ = = '__main__' :
x, y = 5 , 17
print (minimumCost(x, y))
|
C#
using System;
class GFG
{
static int minimumCost( int x, int y)
{
int costx = 0, dup_x = x;
while ( true )
{
int costy = 0, dup_y = y;
while (dup_y != dup_x
&& dup_y < dup_x)
{
dup_y += y;
costy++;
}
if (dup_x == dup_y)
return costx + costy;
else {
dup_x += x;
costx++;
}
}
}
public static void Main(String[] args)
{
int x = 5, y = 17;
Console.Write(minimumCost(x, y) + "\n" );
}
}
|
Javascript
<script>
function minimumCost(x, y)
{
var costx = 0, dup_x = x;
while ( true )
{
var costy = 0, dup_y = y;
while (dup_y != dup_x && dup_y < dup_x) {
dup_y += y;
costy++;
}
if (dup_x == dup_y)
return costx + costy;
else {
dup_x += x;
costx++;
}
}
}
var x = 5, y = 17;
document.write(minimumCost(x, y) + "\n" );
</script>
|
Time Complexity: O((costx + costy)*Y)
Auxiliary Space: O(1)
Efficient Method: The idea here is to use the concept of LCM. The minimum cost of making both X and Y will be equal to their LCM. But this is not enough. Subtract initial values of X and Y to avoid adding them while calculating answers.
Follow the steps below to solve the problem:
- Find LCM of both X and Y.
- Subtract the value of X and Y from LCM.
- Now, divide the LCM by both X and Y, and the sum of their values is the required answer.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int gcd( int x, int y)
{
if (y == 0)
return x;
return gcd(y, x % y);
}
int lcm( int x, int y)
{
return (x * y) / gcd(x, y);
}
int minimumCost( int x, int y)
{
int lcm_ = lcm(x, y);
int costx = (lcm_ - x) / x;
int costy = (lcm_ - y) / y;
return costx + costy;
}
int main()
{
int x = 5, y = 17;
cout << minimumCost(x, y) << endl;
}
|
Java
import java.util.*;
class GFG
{
static int gcd( int x, int y)
{
if (y == 0 )
return x;
return gcd(y, x % y);
}
static int lcm( int x, int y)
{
return (x * y) / gcd(x, y);
}
static int minimumCost( int x, int y)
{
int lcm_ = lcm(x, y);
int costx = (lcm_ - x) / x;
int costy = (lcm_ - y) / y;
return costx + costy;
}
public static void main(String[] args)
{
int x = 5 , y = 17 ;
System.out.print(minimumCost(x, y) + "\n" );
}
}
|
Python3
def gcd(x, y):
if (y = = 0 ):
return x
return gcd(y, x % y)
def lcm(x, y):
return (x * y) / / gcd(x, y)
def minimumCost(x, y):
lcm_ = lcm(x, y)
costx = (lcm_ - x) / / x
costy = (lcm_ - y) / / y
return costx + costy
if __name__ = = "__main__" :
x = 5
y = 17
print (minimumCost(x, y))
|
C#
using System;
class GFG
{
static int gcd( int x, int y)
{
if (y == 0)
return x;
return gcd(y, x % y);
}
static int lcm( int x, int y)
{
return (x * y) / gcd(x, y);
}
static int minimumCost( int x, int y)
{
int lcm_ = lcm(x, y);
int costx = (lcm_ - x) / x;
int costy = (lcm_ - y) / y;
return costx + costy;
}
public static void Main(String[] args)
{
int x = 5, y = 17;
Console.Write(minimumCost(x, y) + "\n" );
}
}
|
Javascript
<script>
function gcd(x , y) {
if (y == 0)
return x;
return gcd(y, x % y);
}
function lcm(x , y) {
return (x * y) / gcd(x, y);
}
function minimumCost(x , y) {
var lcm_ = lcm(x, y);
var costx = (lcm_ - x) / x;
var costy = (lcm_ - y) / y;
return costx + costy;
}
var x = 5, y = 17;
document.write(minimumCost(x, y) + "\n" );
</script>
|
Time Complexity: O(log(min(x, y))
Auxiliary Space: O(1)
Last Updated :
21 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...