Minimum number of given operation required to convert n to m
Given two integers n and m, in a single operation n can be multiplied by either 2 or 3. The task is to convert n to m with a minimum number of given operations. If it is impossible to convert n to m with the given operation then print -1.
Examples:
Input: n = 120, m = 51840
Output: 7
120 * 2 * 2 * 2 * 2 * 3 * 3 * 3 = 51840
Input: n = 42, m = 42
Output: 0
No operation required.
Input: n = 48, m = 72
Output: -1
Approach: If m is not divisible by n then print -1 as n cannot be converted to m with the given operation. Else we can check if, on dividing, the quotient has only 2 and 3 as prime factors. If yes then the result will be the sum of powers of 2 and 3 else print -1
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int n, int m)
{
if (m % n != 0)
return -1;
int minOperations = 0;
int q = m / n;
while (q % 2 == 0) {
q = q / 2;
minOperations++;
}
while (q % 3 == 0) {
q = q / 3;
minOperations++;
}
if (q == 1)
return minOperations;
return -1;
}
int main()
{
int n = 120, m = 51840;
cout << minOperations(n, m);
return 0;
}
|
Java
class GfG {
static int minOperations( int n, int m)
{
if (m % n != 0 )
return - 1 ;
int minOperations = 0 ;
int q = m / n;
while (q % 2 == 0 ) {
q = q / 2 ;
minOperations++;
}
while (q % 3 == 0 ) {
q = q / 3 ;
minOperations++;
}
if (q == 1 )
return minOperations;
return - 1 ;
}
public static void main(String[] args)
{
int n = 120 , m = 51840 ;
System.out.println(minOperations(n, m));
}
}
|
Python3
def minOperations(n, m):
if (m % n ! = 0 ):
return - 1
minOperations = 0
q = int (m / n)
while (q % 2 = = 0 ):
q = int (q / 2 )
minOperations + = 1
while (q % 3 = = 0 ):
q = int (q / 3 )
minOperations + = 1
if (q = = 1 ):
return minOperations
return - 1
if __name__ = = '__main__' :
n = 120
m = 51840
print (minOperations(n, m))
|
C#
using System;
class GFG
{
static int minOperations( int n, int m)
{
if (m % n != 0)
return -1;
int minOperations = 0;
int q = m / n;
while (q % 2 == 0)
{
q = q / 2;
minOperations++;
}
while (q % 3 == 0)
{
q = q / 3;
minOperations++;
}
if (q == 1)
return minOperations;
return -1;
}
public static void Main()
{
int n = 120, m = 51840;
Console.WriteLine(minOperations(n, m));
}
}
|
PHP
<?php
function minOperations( $n , $m )
{
if ( $m % $n != 0)
return -1;
$minOperations = 0;
$q = $m / $n ;
while ( $q % 2 == 0)
{
$q = $q / 2;
$minOperations ++;
}
while ( $q % 3 == 0)
{
$q = $q / 3;
$minOperations ++;
}
if ( $q == 1)
return $minOperations ;
return -1;
}
$n = 120; $m = 51840;
echo (minOperations( $n , $m ));
?>
|
Javascript
<script>
function minOperations(n , m) {
if (m % n != 0)
return -1;
var minOperations = 0;
var q = m / n;
while (q % 2 == 0) {
q = q / 2;
minOperations++;
}
while (q % 3 == 0) {
q = q / 3;
minOperations++;
}
if (q == 1)
return minOperations;
return -1;
}
var n = 120, m = 51840;
document.write(minOperations(n, m));
</script>
|
Time Complexity: O(log(m/n))
Auxiliary Space: O(1)
Last Updated :
09 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...