Minimum multiplications with {2, 3, 7} to make two numbers equal
Last Updated :
23 Jul, 2022
Given two numbers A and B, the task is to find the minimum number of operations required to make A and B equal. In each operation, any number can be divided by either 2, 3 or 7. If it is not possible then print -1.
Examples:
Input: A = 14, B = 28
Output: 1
Operation 1: A * 2 = 14 * 2 = 28 which is equal to B.
Input: A = 3, B = 5
Output: -1
No matter how many times the operation is performed, A and B will never be equal.
Approach: A and B can be written as A = x * 2a1 * 3a2 * 7a3 and B = y * 2b1 * 3b2 * 7b3 where x and y are not divisible by 2, 3 and 7. Now,
- If x != y then A and B cannot be made equal with the given operation.
- If x = y then the minimum operations required will be |a1 – b1| + |a2 – b2| + |a3 – b3| because both the numbers need to have equal powers of 2, 3 and 7.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > Divisors( int x)
{
int c = 0;
vector< int > v;
while (x % 2 == 0) {
c++;
x /= 2;
}
v.push_back(c);
c = 0;
while (x % 3 == 0) {
c++;
x /= 3;
}
v.push_back(c);
c = 0;
while (x % 7 == 0) {
c++;
x /= 7;
}
v.push_back(c);
v.push_back(x);
return v;
}
int MinOperations( int a, int b)
{
vector< int > va = Divisors(a);
vector< int > vb = Divisors(b);
if (va[3] != vb[3])
return -1;
int minOperations = abs (va[0] - vb[0])
+ abs (va[1] - vb[1])
+ abs (va[2] - vb[2]);
return minOperations;
}
int main()
{
int a = 14, b = 28;
cout << MinOperations(a, b);
return 0;
}
|
Java
import java.util.Vector;
class GFG
{
static Vector<Integer> Divisors( int x)
{
int c = 0 ;
Vector<Integer> v = new Vector<Integer>();
while (x % 2 == 0 )
{
c++;
x /= 2 ;
}
v.add(c);
c = 0 ;
while (x % 3 == 0 )
{
c++;
x /= 3 ;
}
v.add(c);
c = 0 ;
while (x % 7 == 0 )
{
c++;
x /= 7 ;
}
v.add(c);
v.add(x);
return v;
}
static int MinOperations( int a, int b)
{
Vector<Integer> va = Divisors(a);
Vector<Integer> vb = Divisors(b);
if (va.get( 3 ) != vb.get( 3 ))
{
return - 1 ;
}
int minOperations = Math.abs(va.get( 0 ) - vb.get( 0 ))
+ Math.abs(va.get( 1 ) - vb.get( 1 ))
+ Math.abs(va.get( 2 ) - vb.get( 2 ));
return minOperations;
}
public static void main(String[] args)
{
int a = 14 , b = 28 ;
System.out.println(MinOperations(a, b));
}
}
|
Python3
def Divisors(x):
c = 0
v = []
while (x % 2 = = 0 ):
c + = 1
x / = 2
v.append(c)
c = 0
while (x % 3 = = 0 ):
c + = 1
x / = 3
v.append(c)
c = 0
while (x % 7 = = 0 ):
c + = 1
x / = 7
v.append(c)
v.append(x)
return v
def MinOperations(a, b):
va = Divisors(a)
vb = Divisors(b)
if (va[ 3 ] ! = vb[ 3 ]):
return - 1
minOperations = abs (va[ 0 ] - vb[ 0 ]) + abs (va[ 1 ] - vb[ 1 ]) + abs (va[ 2 ] - vb[ 2 ])
return minOperations
if __name__ = = '__main__' :
a = 14
b = 28
print (MinOperations(a, b))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static List< int > Divisors( int x)
{
int c = 0;
List< int > v = new List< int >();
while (x % 2 == 0)
{
c++;
x /= 2;
}
v.Add(c);
c = 0;
while (x % 3 == 0)
{
c++;
x /= 3;
}
v.Add(c);
c = 0;
while (x % 7 == 0)
{
c++;
x /= 7;
}
v.Add(c);
v.Add(x);
return v;
}
static int MinOperations( int a, int b)
{
List< int > va = Divisors(a);
List< int > vb = Divisors(b);
if (va[3] != vb[3])
{
return -1;
}
int minOperations = Math.Abs(va[0] - vb[0]) +
Math.Abs(va[1] - vb[1]) +
Math.Abs(va[2] - vb[2]);
return minOperations;
}
public static void Main(String[] args)
{
int a = 14, b = 28;
Console.WriteLine(MinOperations(a, b));
}
}
|
PHP
<?php
function Divisors( $x )
{
$c = 0;
$v = array ();
while ( $x % 2 == 0)
{
$c ++;
$x = floor ( $x / 2);
}
array_push ( $v , $c );
$c = 0;
while ( $x % 3 == 0)
{
$c ++;
$x = floor ( $x / 3);
}
array_push ( $v , $c ) ;
$c = 0;
while ( $x % 7 == 0)
{
$c ++;
$x = floor ( $x / 7);
}
array_push ( $v , $c );
array_push ( $v , $x );
return $v ;
}
function MinOperations( $a , $b )
{
$va = Divisors( $a );
$vb = Divisors( $b );
if ( $va [3] != $vb [3])
return -1;
$minOperations = abs ( $va [0] - $vb [0]) +
abs ( $va [1] - $vb [1]) +
abs ( $va [2] - $vb [2]);
return $minOperations ;
}
$a = 14 ;
$b = 28 ;
echo MinOperations( $a , $b );
?>
|
Javascript
<script>
function Divisors(x)
{
var c = 0;
var v = [];
while (x % 2 == 0) {
c++;
x /= 2;
}
v.push(c);
c = 0;
while (x % 3 == 0) {
c++;
x /= 3;
}
v.push(c);
c = 0;
while (x % 7 == 0) {
c++;
x /= 7;
}
v.push(c);
v.push(x);
return v;
}
function MinOperations(a , b) {
var va = Divisors(a);
var vb = Divisors(b);
if (va[3] != vb[3]) {
return -1;
}
var minOperations = Math.abs(va[0] - vb[0]) + Math.abs(va[1] - vb[1])
+ Math.abs(va[2] - vb[2]);
return minOperations;
}
var a = 14, b = 28;
document.write(MinOperations(a, b));
</script>
|
Time Complexity: O(log2x) + O(log3x) + O(log7x)
Auxiliary Space: O(c), where c is the count of each divisor
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...