给定两个整数X和Y ,任务是通过多次执行以下操作来使两个整数相等:
- 将X增加M倍。成本= M – 1 。
- 将Y增加N倍。成本= N – 1 。
例子:
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.
天真的方法:请按照以下步骤解决问题:
- 对于X的每个值,如果Y小于X ,则增加Y并更新成本。
- 如果X = Y ,则打印总成本。
- 如果X
,则增加X并更新成本。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum cost
// to make x and y equal
int minimumCost(int x, int y)
{
int costx = 0, dup_x = x;
while (true) {
int costy = 0, dup_y = y;
// Check if it possible
// to make x == y
while (dup_y != dup_x
&& dup_y < dup_x) {
dup_y += y;
costy++;
}
// Iif both are equal
if (dup_x == dup_y)
return costx + costy;
// Otherwise
else {
// Increment cost of x
// by 1 and dup_x by x
dup_x += x;
costx++;
}
}
}
// Driver Code
int main()
{
int x = 5, y = 17;
// Returns the required minimum cost
cout << minimumCost(x, y) << endl;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find minimum cost
// to make x and y equal
static int minimumCost(int x, int y)
{
int costx = 0, dup_x = x;
while (true)
{
int costy = 0, dup_y = y;
// Check if it possible
// to make x == y
while (dup_y != dup_x
&& dup_y < dup_x)
{
dup_y += y;
costy++;
}
// Iif both are equal
if (dup_x == dup_y)
return costx + costy;
// Otherwise
else {
// Increment cost of x
// by 1 and dup_x by x
dup_x += x;
costx++;
}
}
}
// Driver Code
public static void main(String[] args)
{
int x = 5, y = 17;
// Returns the required minimum cost
System.out.print(minimumCost(x, y) +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to find minimum cost
# to make x and y equal
def minimumCost(x, y):
costx, dup_x = 0, x
while (True):
costy, dup_y = 0, y
# Check if it possible
# to make x == y
while (dup_y != dup_x and
dup_y < dup_x):
dup_y += y
costy += 1
# If both are equal
if (dup_x == dup_y):
return costx + costy
# Otherwise
else:
# Increment cost of x
# by 1 and dup_x by x
dup_x += x
costx += 1
# Driver Code
if __name__ == '__main__':
x, y = 5, 17
# Returns the required minimum cost
print(minimumCost(x, y))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find minimum cost
// to make x and y equal
static int minimumCost(int x, int y)
{
int costx = 0, dup_x = x;
while (true)
{
int costy = 0, dup_y = y;
// Check if it possible
// to make x == y
while (dup_y != dup_x
&& dup_y < dup_x)
{
dup_y += y;
costy++;
}
// Iif both are equal
if (dup_x == dup_y)
return costx + costy;
// Otherwise
else {
// Increment cost of x
// by 1 and dup_x by x
dup_x += x;
costx++;
}
}
}
// Driver Code
public static void Main(String[] args)
{
int x = 5, y = 17;
// Returns the required minimum cost
Console.Write(minimumCost(x, y) +"\n");
}
}
// This code is contributed by 29AjayKumar
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find gcd of x and y
int gcd(int x, int y)
{
if (y == 0)
return x;
return gcd(y, x % y);
}
// Function to find lcm of x and y
int lcm(int x, int y)
{
return (x * y) / gcd(x, y);
}
// Function to find minimum Cost
int minimumCost(int x, int y)
{
int lcm_ = lcm(x, y);
// Subtracted intial cost of x
int costx = (lcm_ - x) / x;
// Subtracted intial cost of y
int costy = (lcm_ - y) / y;
return costx + costy;
}
// Driver Code
int main()
{
int x = 5, y = 17;
// Returns the minimum cost required
cout << minimumCost(x, y) << endl;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find gcd of x and y
static int gcd(int x, int y)
{
if (y == 0)
return x;
return gcd(y, x % y);
}
// Function to find lcm of x and y
static int lcm(int x, int y)
{
return (x * y) / gcd(x, y);
}
// Function to find minimum Cost
static int minimumCost(int x, int y)
{
int lcm_ = lcm(x, y);
// Subtracted intial cost of x
int costx = (lcm_ - x) / x;
// Subtracted intial cost of y
int costy = (lcm_ - y) / y;
return costx + costy;
}
// Driver Code
public static void main(String[] args)
{
int x = 5, y = 17;
// Returns the minimum cost required
System.out.print(minimumCost(x, y) +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to find gcd of x and y
def gcd(x, y):
if (y == 0):
return x
return gcd(y, x % y)
# Function to find lcm of x and y
def lcm(x, y):
return (x * y) // gcd(x, y)
# Function to find minimum Cost
def minimumCost(x, y):
lcm_ = lcm(x, y)
# Subtracted intial cost of x
costx = (lcm_ - x) // x
# Subtracted intial cost of y
costy = (lcm_ - y) // y
return costx + costy
# Driver Code
if __name__ == "__main__":
x = 5
y = 17
# Returns the minimum cost required
print(minimumCost(x, y))
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find gcd of x and y
static int gcd(int x, int y)
{
if (y == 0)
return x;
return gcd(y, x % y);
}
// Function to find lcm of x and y
static int lcm(int x, int y)
{
return (x * y) / gcd(x, y);
}
// Function to find minimum Cost
static int minimumCost(int x, int y)
{
int lcm_ = lcm(x, y);
// Subtracted intial cost of x
int costx = (lcm_ - x) / x;
// Subtracted intial cost of y
int costy = (lcm_ - y) / y;
return costx + costy;
}
// Driver Code
public static void Main(String[] args)
{
int x = 5, y = 17;
// Returns the minimum cost required
Console.Write(minimumCost(x, y) +"\n");
}
}
// This code is contributed by 29AjayKumar
输出:
20
时间复杂度: O((costx + costy)* Y)
辅助空间: O(1)
高效的方法:这里的想法是使用LCM的概念。制作X和Y的最低成本将等于其LCM。但这还不够。减去X和Y的初始值,以避免在计算答案时将它们相加。
请按照以下步骤解决问题:
- 查找X和Y的LCM。
- 从LCM中减去X和Y的值。
- 现在,将LCM除以X和Y ,然后求和它们的值之和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find gcd of x and y
int gcd(int x, int y)
{
if (y == 0)
return x;
return gcd(y, x % y);
}
// Function to find lcm of x and y
int lcm(int x, int y)
{
return (x * y) / gcd(x, y);
}
// Function to find minimum Cost
int minimumCost(int x, int y)
{
int lcm_ = lcm(x, y);
// Subtracted intial cost of x
int costx = (lcm_ - x) / x;
// Subtracted intial cost of y
int costy = (lcm_ - y) / y;
return costx + costy;
}
// Driver Code
int main()
{
int x = 5, y = 17;
// Returns the minimum cost required
cout << minimumCost(x, y) << endl;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find gcd of x and y
static int gcd(int x, int y)
{
if (y == 0)
return x;
return gcd(y, x % y);
}
// Function to find lcm of x and y
static int lcm(int x, int y)
{
return (x * y) / gcd(x, y);
}
// Function to find minimum Cost
static int minimumCost(int x, int y)
{
int lcm_ = lcm(x, y);
// Subtracted intial cost of x
int costx = (lcm_ - x) / x;
// Subtracted intial cost of y
int costy = (lcm_ - y) / y;
return costx + costy;
}
// Driver Code
public static void main(String[] args)
{
int x = 5, y = 17;
// Returns the minimum cost required
System.out.print(minimumCost(x, y) +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to find gcd of x and y
def gcd(x, y):
if (y == 0):
return x
return gcd(y, x % y)
# Function to find lcm of x and y
def lcm(x, y):
return (x * y) // gcd(x, y)
# Function to find minimum Cost
def minimumCost(x, y):
lcm_ = lcm(x, y)
# Subtracted intial cost of x
costx = (lcm_ - x) // x
# Subtracted intial cost of y
costy = (lcm_ - y) // y
return costx + costy
# Driver Code
if __name__ == "__main__":
x = 5
y = 17
# Returns the minimum cost required
print(minimumCost(x, y))
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find gcd of x and y
static int gcd(int x, int y)
{
if (y == 0)
return x;
return gcd(y, x % y);
}
// Function to find lcm of x and y
static int lcm(int x, int y)
{
return (x * y) / gcd(x, y);
}
// Function to find minimum Cost
static int minimumCost(int x, int y)
{
int lcm_ = lcm(x, y);
// Subtracted intial cost of x
int costx = (lcm_ - x) / x;
// Subtracted intial cost of y
int costy = (lcm_ - y) / y;
return costx + costy;
}
// Driver Code
public static void Main(String[] args)
{
int x = 5, y = 17;
// Returns the minimum cost required
Console.Write(minimumCost(x, y) +"\n");
}
}
// This code is contributed by 29AjayKumar
输出:
20
时间复杂度: O(log(min(x,y))
辅助空间: O(1)