Min operations to reduce N to 1 by multiplying by A or dividing by B
Last Updated :
11 May, 2021
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”.
Examples:
Input: N = 48, A = 3, B = 12
Output: 3
Explanation:
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
Output: -1
Explanation:
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:
C++
#include <iostream>
using namespace std;
int findIfPossible( int n, int a, int b)
{
if (b % a != 0) {
if (n == 1)
return 0;
else if (n % b != 0)
return -1;
else
return ( int )n / b;
}
int c = b / a;
int x = 0, y = 0;
while (n % b == 0) {
n = n / b;
x++;
}
while (n % c == 0) {
n = n / c;
y++;
}
if (n == 1) {
int total_steps = x + (2 * y);
return total_steps;
}
else
return -1;
}
int main()
{
int n = 48;
int a = 3, b = 12;
cout << findIfPossible(n, a, b);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int findIfPossible( int n, int a, int b)
{
if (b % a != 0 )
{
if (n == 1 )
return 0 ;
else if (n % b != 0 )
return - 1 ;
else
return ( int )n / b;
}
int c = b / a;
int x = 0 , y = 0 ;
while (n % b == 0 )
{
n = n / b;
x++;
}
while (n % c == 0 )
{
n = n / c;
y++;
}
if (n == 1 )
{
int total_steps = x + ( 2 * y);
return total_steps;
}
else
return - 1 ;
}
public static void main(String s[])
{
int n = 48 ;
int a = 3 , b = 12 ;
System.out.println(findIfPossible(n, a, b));
}
}
|
Python3
def FindIfPossible(n, a, b):
if (b % a) ! = 0 :
if n = = 1 :
return 0
elif (n % b) ! = 0 :
return - 1
else :
return int (n / b)
c = b / a
x = 0
y = 0
while (n % b = = 0 ):
n / = b
x + = 1
while (n % c = = 0 ):
n / = c
y + = 1
if n = = 1 :
total_steps = x + 2 * y
return total_steps
else :
return - 1
n = 48
a = 3
b = 12
print (FindIfPossible(n, a, b))
|
C#
using System;
class GFG{
static int findIfPossible( int n, int a, int b)
{
if (b % a != 0)
{
if (n == 1)
return 0;
else if (n % b != 0)
return -1;
else
return ( int )n / b;
}
int c = b / a;
int x = 0, y = 0;
while (n % b == 0)
{
n = n / b;
x++;
}
while (n % c == 0)
{
n = n / c;
y++;
}
if (n == 1)
{
int total_steps = x + (2 * y);
return total_steps;
}
else
return -1;
}
public static void Main()
{
int n = 48;
int a = 3, b = 12;
Console.WriteLine(findIfPossible(n, a, b));
}
}
|
Javascript
<script>
function findIfPossible(n, a, b)
{
if (b % a != 0)
{
if (n == 1)
return 0;
else if (n % b != 0)
return -1;
else
return n / b;
}
let c = b / a;
let x = 0, y = 0;
while (n % b == 0)
{
n = n / b;
x++;
}
while (n % c == 0)
{
n = n / c;
y++;
}
if (n == 1)
{
let total_steps = x + (2 * y);
return total_steps;
}
else
return -1;
}
let n = 48;
let a = 3, b = 12;
document.write(findIfPossible(n, a, b));
</script>
|
Time Complexity: O(log (B/A))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...