Given two integers X and Y, the task is to find the minimum number of steps to convert integer X to Y using any of the operations in each step:
- Divide the number by any natural number
- Multiply the number with any natural number
Examples:
Input: X = 8, Y = 12
Output: 2
Explanation:
First divide 8 by 2: 8/2 = 4
Then multiply by 3: 4*3 = 12
Input: X = 4, Y = 8
Output: 1
Explanation:
To convert 4 to 8 multiply 4 by 2: 4 * 2 = 8
Approach: To solve the problem mentioned above:
- Make sure X contains the smaller value among X and Y. Now, if X is greater than Y then we know that it is always easier to change a smaller number to a larger number. Hence, we just swap the values of X and Y and then follow the steps mentioned below.
- If both the integers are the same then the answer will be zero as no conversion takes place.
For example,
If X = 4, Y = 4
Here 4 = 4
Therefore, answer = 0
(as they both are already same)
- However, if X is less than Y then:
- we have to check that Y % X gives 0 or not.
- If yes, then Y can be represented as X * (Y / X) and we get the desired output in single-step.
For example,
If X = 4, Y = 12
Here 12 % 4 = 0
Therefore, answer = 1
(4 * 3 = 12)
- Otherwise, the answer will be 2 as it takes two steps, one for division (X = X/X) and the other for multiplication (X = X * Y).
For example,
If X = 8, Y = 13
Here 13 % 8 != 0
Therefore,
1. X = X/X = 8/8 = 1
2. X = X*Y = 1*13 = 13
Hence, answer = 2
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int X, int Y)
{
if (X > Y) {
int temp = X;
X = Y;
Y = temp;
}
if (X == Y)
cout << 0 << endl;
else if (Y % X == 0)
cout << 1 << endl;
else
cout << 2 << endl;
}
int main()
{
int X = 8, Y = 13;
solve(X, Y);
return 0;
}
|
Java
class GFG{
static int solve( int X, int Y)
{
if (X > Y)
{
int temp = X;
X = Y;
Y = temp;
}
if (X == Y)
System.out.println( 0 );
else if (Y % X == 0 )
System.out.println( 1 );
else
System.out.println( 2 );
return 0 ;
}
public static void main(String args[])
{
int X = 8 , Y = 13 ;
solve(X, Y);
}
}
|
Python3
def solve(X, Y):
if (X > Y):
temp = X
X = Y
Y = temp
if (X = = Y):
print ( 0 )
elif (Y % X = = 0 ):
print ( 1 )
else :
print ( 2 )
X = 8
Y = 13
solve(X, Y)
|
C#
using System;
class GFG{
static int solve( int X, int Y)
{
if (X > Y)
{
int temp = X;
X = Y;
Y = temp;
}
if (X == Y)
Console.WriteLine(0);
else if (Y % X == 0)
Console.WriteLine(1);
else
Console.WriteLine(2);
return 0;
}
public static void Main(String[] args)
{
int X = 8, Y = 13;
solve(X, Y);
}
}
|
Javascript
<script>
function solve(X, Y)
{
if (X > Y) {
let temp = X;
X = Y;
Y = temp;
}
if (X == Y)
document.write(0);
else if (Y % X == 0)
document.write(1);
else
document.write(2);
}
let X = 8, Y = 13;
solve(X, Y);
</script>
|
Time Complexity: O(1), as we are using only constant-time operations.
Auxiliary Space: O(1), as we are not using any extra space.
Method 2: Use the concept of the Greatest Common Divisor (GCD) of X and Y.
In this approach, we first calculate the GCD of X and Y using the math.gcd() function. Then, we divide X by the GCD to get a reduced value. We then repeatedly divide the reduced value by 2 until we get 1, and count the number of steps taken. Finally, we add 1 to the step count to account for the multiplication step required to convert the reduced value back to Y.
C++
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void solve( int X, int Y) {
int gcd = __gcd(X, Y);
X /= gcd;
int steps = 0;
while (X > 1) {
X /= 2;
steps++;
}
cout << steps + 1 << endl;
}
int main() {
int X = 8, Y = 12;
solve(X, Y);
return 0;
}
|
Python3
import math
def solve(X, Y):
gcd = math.gcd(X, Y)
X / / = gcd
steps = 0
while X > 1 :
X / / = 2
steps + = 1
print (steps + 1 )
X = 8
Y = 12
solve(X, Y)
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
int X = 8 , Y = 12 ;
solve(X, Y);
}
public static void solve( int X, int Y) {
int gcd = gcd(X, Y);
X /= gcd;
int steps = 0 ;
while (X > 1 ) {
X /= 2 ;
steps++;
}
System.out.println(steps + 1 );
}
public static int gcd( int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
|
C#
using System;
class Program
{
static void Main( string [] args)
{
int X = 8, Y = 12;
solve(X, Y);
}
static void solve( int X, int Y)
{
int gcd = MathExt.Gcd(X, Y);
X /= gcd;
int steps = 0;
while (X > 1)
{
X /= 2;
steps++;
}
Console.WriteLine(steps + 1);
}
}
public static class MathExt
{
public static int Gcd( int a, int b)
{
return b == 0 ? a : Gcd(b, a % b);
}
}
|
Javascript
function solve(X, Y) {
const gcd = Gcd(X, Y);
X /= gcd;
let steps = 0;
while (X > 1) {
X /= 2;
steps++;
}
console.log(steps + 1);
}
function Gcd(a, b) {
return b == 0 ? a : Gcd(b, a % b);
}
const X = 8, Y = 12;
solve(X, Y);
|
The time complexity of the provided function is O(log(min(X, Y))) because we are performing division by 2 repeatedly until X reaches 1, and the number of times we need to perform this operation is proportional to the logarithm of X.
The auxiliary space of the function is O(1) because we are using a constant amount of memory to store the variables X, Y, gcd, and steps. The space required for the math module is not considered because it is a built-in module and its space is already reserved by the Python interpreter.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
24 Mar, 2023
Like Article
Save Article