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.
Input: X = 12, Y = 2592, P = 2, Q = 3
(12 * 2) -> (24 * 3) -> (72 * 2) -> (144 * 3) -> (432 * 3) -> (1296 * 2) ->2592
Input: X = 7, Y = 9, P = 2, Q = 7
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Min operations to reduce N to 1 by multiplying by A or dividing by B
- Min operations to reduce N by multiplying by any number or taking square root
- Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1
- Minimum operations required to convert all characters of a String to a given Character
- Minimum number operations required to convert n to m | Set-2
- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Check if elements of array can be made equal by multiplying given prime numbers
- Minimum number of steps required to obtain the given Array by the given operations
- Find the number of consecutive zero at the end after multiplying n numbers
- Reach A and B by multiplying them with K and K^2 at every step
- Maximize the sum of array by multiplying prefix of array with -1
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Maximize the sum of array after multiplying a prefix and suffix by -1
- Check if N can be obtained from 1 by repetitively multiplying by 10 or 20
- Maximum subsequence sum possible by multiplying each element by its index
- Print Matrix after multiplying Matrix elements N times
- Minimum operations of the given type required to make a complete graph
- Minimum number of given operations required to reduce the array to 0 element
- Minimum number of operations required to obtain a given Binary String
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.