Given a number N and two integers A and B, the task is to check if it is possible to convert the number to 1 by the following two operations:
- Multiply it by A
- Divide it by B
If it is possible to reduce N to 1 then print the minimum number of operations required to achieve it otherwise print “-1”.
Input: N = 48, A = 3, B = 12
Below are the 3 operations:
1. Divide 48 by 12 to get 4.
2. Multiply 4 by 3 to get 12.
3.Divide 12 by 12 to get 1.
Hence the total number of operation is 3.
Input: N = 26, A = 3, B = 9
It is not possible to convert 26 to 1.
Approach: The problem can be solved using Greedy Approach. The idea is to check if B is divisible by A or not and on the basis of that we have the below observations:
- If B%A != 0, then it is only possible to convert N to 1 if N is completely divisible by B and it would require N/B steps to do so. whereas if N = 1 then it would require 0 steps, otherwise it’s impossible and prints “-1”.
- If B%A == 0, then consider a variable C whose value is B/A. Divide N by B, using the second operation until it cannot be divided any further, let’s call the number of division as x.
- Again divide the remaining N by C until it cannot be divided any further, let’s call the number of divisions in this operation be y.
- If N does not equal 1 after the above operations then it is impossible to convert N to 1 using the above-mentioned operations and the answer will be “-1”, but if it is equal to 1 then we can use the formula total_steps = x + (2 * y) to calculate the total minimum steps required.
Below is the implementation of the above approach:
Time Complexity: O(log (B/A))
Auxiliary Space: O(1)
- Min operations to reduce N by multiplying by any number or taking square root
- Min number of operations to reduce N to 0 by subtracting any digits from N
- Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1
- Minimum operations required to convert X to Y by multiplying X with the given co-primes
- Minimum number of operations required to reduce N to 1
- Count operations of the given type required to reduce N to 0
- Find maximum operations to reduce N to 1
- Count the number of operations required to reduce the given number
- Minimum number of given operations required to reduce the array to 0 element
- Reduce N to 1 with minimum number of given operations
- Reduce a number to 1 by performing given operations | Set 2
- Minimum number of operations required to reduce N to 0
- Minimum steps to reduce N to 0 by given operations
- Reduce a given number to form a key by the given operations
- Reduce N to 0 or less by given X and Y operations
- Check if elements of array can be made equal by multiplying given prime numbers
- 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
- Find Kth number from sorted array formed by multiplying any two numbers in the array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.