Minimum operations required to convert X to Y by multiplying X with the given co-primes
Given four integers X, Y, P and Q such that X ? Y and gcd(P, Q) = 1. The task is to find minimum operation required to convert X to Y. In a single operation, you can multiply X with either P or Q. If it is not possible to convert X to Y then print -1.
Examples:
Input: X = 12, Y = 2592, P = 2, Q = 3
Output: 6
(12 * 2) -> (24 * 3) -> (72 * 2) -> (144 * 3) -> (432 * 3) -> (1296 * 2) ->2592
Input: X = 7, Y = 9, P = 2, Q = 7
Output: -1
There is no way we can reach 9 from 7 by
multiplying 7 with either 2 or 7
Approach: If Y is not divisible by X then no integral multiplication of any integer with X any number of times can lead to Y and the result is -1.
Else, let d = Y / X. Now, Pa * Qb = d must hold in order to have a valid solution and the result in that case will be (a + b) else -1 if d cannot be expressed in the powers of P and Q.
In order to check the condition, keep dividing d with P and Q until divisible. Now, if d = 1 in the end then the solution is possible else not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int x, int y, int p, int q)
{
if (y % x != 0)
return -1;
int d = y / x;
int a = 0;
while (d % p == 0) {
d /= p;
a++;
}
int b = 0;
while (d % q == 0) {
d /= q;
b++;
}
if (d != 1)
return -1;
return (a + b);
}
int main()
{
int x = 12, y = 2592, p = 2, q = 3;
cout << minOperations(x, y, p, q);
return 0;
}
|
Java
class GFG
{
static int minOperations( int x, int y, int p, int q)
{
if (y % x != 0 )
return - 1 ;
int d = y / x;
int a = 0 ;
while (d % p == 0 )
{
d /= p;
a++;
}
int b = 0 ;
while (d % q == 0 )
{
d /= q;
b++;
}
if (d != 1 )
return - 1 ;
return (a + b);
}
public static void main (String[] args)
{
int x = 12 , y = 2592 , p = 2 , q = 3 ;
System.out.println(minOperations(x, y, p, q));
}
}
|
Python3
def minOperations(x, y, p, q):
if (y % x ! = 0 ):
return - 1
d = y / / x
a = 0
while (d % p = = 0 ):
d / / = p
a + = 1
b = 0
while (d % q = = 0 ):
d / / = q
b + = 1
if (d ! = 1 ):
return - 1
return (a + b)
x = 12
y = 2592
p = 2
q = 3
print (minOperations(x, y, p, q))
|
C#
using System;
class GFG
{
static int minOperations( int x, int y, int p, int q)
{
if (y % x != 0)
return -1;
int d = y / x;
int a = 0;
while (d % p == 0)
{
d /= p;
a++;
}
int b = 0;
while (d % q == 0)
{
d /= q;
b++;
}
if (d != 1)
return -1;
return (a + b);
}
public static void Main ()
{
int x = 12, y = 2592, p = 2, q = 3;
Console.Write(minOperations(x, y, p, q));
}
}
|
Javascript
<script>
function minOperations(x, y, p, q){
if (y % x != 0)
return -1
var d = Math.floor(y / x)
var a = 0
while (d % p == 0){
d = Math.floor(d / p)
a+=1
}
var b = 0
while (d % q == 0){
d = Math.floor(d / q)
b+=1
}
if (d != 1)
return -1
return (a + b)
}
var x = 12
var y = 2592
var p = 2
var q = 3
document.write(minOperations(x, y, p, q))
</script>
|
Auxiliary Space: O(1), since no extra space has been taken
Last Updated :
06 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...