Minimum multiplications with {2, 3, 7} to make two numbers equal

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find powers of 2, 3 and 7 in x
vector<int> Divisors(int x)
{
    // To keep count of each divisor
    int c = 0;
  
    // To store the result
    vector<int> v;
  
    // Count powers of 2 in x
    while (x % 2 == 0) {
        c++;
        x /= 2;
    }
    v.push_back(c);
  
    c = 0;
  
    // Count powers of 3 in x
    while (x % 3 == 0) {
        c++;
        x /= 3;
    }
    v.push_back(c);
  
    c = 0;
  
    // Count powers of 7 in x
    while (x % 7 == 0) {
        c++;
        x /= 7;
    }
    v.push_back(c);
  
    // Reamining number which is not
    // divisible by 2, 3 or 7
    v.push_back(x);
  
    return v;
}
  
// Function to return the minimum number of
// given operations required to make a and b equal
int MinOperations(int a, int b)
{
    // a = x * 2^a1 * 3^a2 * 7^a3
    // va[0] = a1
    // va[1] = a2
    // va[2] = a3
    // va[3] = x
    vector<int> va = Divisors(a);
  
    // Similarly for b
    vector<int> vb = Divisors(b);
  
    // If a and b cannot be made equal
    // with the given operation. Note
    // that va[3] and vb[3] contain
    // remaining numbers after repeated 
    // divisions with 2, 3 and 7.
    // If remaining numbers are not same
    // then we cannot make them equal.
    if (va[3] != vb[3])
        return -1;
  
    // Minimum number of operations required
    int minOperations = abs(va[0] - vb[0])
                        + abs(va[1] - vb[1])
                        + abs(va[2] - vb[2]);
  
    return minOperations;
}
  
// Driver code
int main()
{
    int a = 14, b = 28;
    cout << MinOperations(a, b);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# python 3 implementation of the approach
  
# Function to find powers of 2, 3 and 7 in x
def Divisors(x):
    # To keep count of each divisor
    c = 0
  
    # To store the result
    v = []
  
    # Count powers of 2 in x
    while (x % 2 == 0):
        c += 1
        x /= 2
  
    v.append(c)
  
    c = 0
  
    # Count powers of 3 in x
    while (x % 3 == 0):
        c += 1
        x /= 3
  
    v.append(c)
  
    c = 0
  
    # Count powers of 7 in x
    while (x % 7 == 0):
        c += 1
        x /= 7
  
    v.append(c)
  
    # Reamining number which is not
    # divisible by 2, 3 or 7
    v.append(x)
  
    return v
  
# Function to return the minimum number of
# given operations required to make a and b equal
def MinOperations(a, b):
    # a = x * 2^a1 * 3^a2 * 7^a3
    # va[0] = a1
    # va[1] = a2
    # va[2] = a3
    # va[3] = x
    va = Divisors(a)
  
    # Similarly for b
    vb = Divisors(b)
  
    # If a and b cannot be made equal
    # with the given operation. Note
    # that va[3] and vb[3] contain
    # remaining numbers after repeated 
    # divisions with 2, 3 and 7.
    # If remaining numbers are not same
    # then we cannot make them equal.
    if (va[3] != vb[3]):
        return -1
  
    # Minimum number of operations required
    minOperations = abs(va[0] - vb[0]) + abs(va[1] - vb[1]) + abs(va[2] - vb[2])
  
    return minOperations
  
# Driver code
if __name__ == '__main__':
    a = 14
    b = 28
    print(MinOperations(a, b))
  
# This code is contributed by
# Sanjit_Prasad

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach 
  
// Function to find powers of 2, 3 and 7 in x 
function Divisors($x
    // To keep count of each divisor 
    $c = 0; 
  
    // To store the result 
    $v = array(); 
  
    // Count powers of 2 in x 
    while ($x % 2 == 0) 
    
        $c++; 
        $x = floor($x / 2); 
    
    array_push($v, $c);
  
    $c = 0; 
  
    // Count powers of 3 in x 
    while ($x % 3 == 0)
    
        $c++; 
        $x = floor($x / 3); 
    
    array_push($v, $c) ;
  
    $c = 0; 
  
    // Count powers of 7 in x 
    while ($x % 7 == 0) 
    
        $c++; 
        $x = floor($x / 7); 
    
    array_push($v, $c); 
  
    // Reamining number which is not 
    // divisible by 2, 3 or 7 
    array_push($v, $x); 
  
    return $v
  
// Function to return the minimum number 
// of given operations required to make 
// a and b equal 
function MinOperations($a, $b
      
    // a = x * 2^a1 * 3^a2 * 7^a3 
    // va[0] = a1 
    // va[1] = a2 
    // va[2] = a3 
    // va[3] = x 
    $va = Divisors($a); 
  
    // Similarly for b 
    $vb = Divisors($b); 
  
    // If a and b cannot be made equal 
    // with the given operation. Note 
    // that va[3] and vb[3] contain 
    // remaining numbers after repeated 
    // divisions with 2, 3 and 7. 
    // If remaining numbers are not same 
    // then we cannot make them equal. 
    if ($va[3] != $vb[3]) 
        return -1; 
  
    // Minimum number of operations required 
    $minOperations = abs($va[0] - $vb[0]) + 
                     abs($va[1] - $vb[1]) + 
                     abs($va[2] - $vb[2]); 
  
    return $minOperations
  
// Driver code 
$a = 14 ;
$b = 28 ;
echo MinOperations($a, $b); 
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

1


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Improved By : Sanjit_Prasad, Ryuga