Skip to content
Related Articles

Related Articles

Minimize divisions by 2, 3, or 5 required to make two given integers equal
  • Last Updated : 16 Apr, 2021

Given two integers X and Y, the task is to make X and Y equal by dividing either of X or Y by 2, 3, or 5, minimum number of times, if found to be divisible. If the two integers can be made equal, then print “-1”.

Examples:

Input: X = 15, Y = 20
Output: 3
Explanation:
Operation 1: Reduce X(= 15) to 15/3 i.e., X = 15/3 = 5.
Operation 2: Reduce Y(= 20) to 20/2 i.e., Y = 20/2 = 10.
Operation 3: Reduce Y(= 20) to 10/2 i.e., Y = 10/2 = 5.
After the above operations, X and Y are equal i.e., 5.
Therefore, the count of operation required is 3.

Input: X = 6, Y = 6
Output: 0

Approach: The given problem can be solved greedily. The idea is to reduce the given integers to their GCD in order to make them equal. Follow the steps below to solve the problem:



  • Find the Greatest Common Divisor (GCD) of X and Y and divide X and Y by their GCD.
  • Initialize a variable, say count, to store the number of divisions required.
  • Iterate until X is not equal to Y and perform the following steps: 
    • If the value of X is greater than Y, then swap X and Y.
    • If the larger number(i.e., Y) is divisible by 2, 3, or 5, then divide it by that number. Increment count by 1. Otherwise, if it is not possible to make both the numbers equal, print “-1” and break out of the loop.
  • After completing the above steps, if both the numbers can be made equal, then print the value of count as the minimum number of divisions required to make X and Y equal.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate
// GCD of two numbers
int gcd(int a, int b)
{
    // Base Case
    if (b == 0) {
        return a;
    }
 
    // Calculate GCD recursively
    return gcd(b, a % b);
}
 
// Function to count the minimum
// number of divisions required
// to make X and Y equal
void minimumOperations(int X, int Y)
{
    // Calculate GCD of X and Y
    int GCD = gcd(X, Y);
 
    // Divide X and Y by their GCD
    X = X / GCD;
    Y = Y / GCD;
 
    // Stores the number of divisions
    int count = 0;
 
    // Iterate until X != Y
    while (X != Y) {
 
        // Maintain the order X <= Y
        if (Y > X) {
            swap(X, Y);
        }
 
        // If X is divisible by 2,
        // then divide X by 2
        if (X % 2 == 0) {
            X = X / 2;
        }
 
        // If X is divisible by 3,
        // then divide X by 3
        else if (X % 3 == 0) {
            X = X / 3;
        }
 
        // If X is divisible by 5,
        // then divide X by 5
        else if (X % 5 == 0) {
            X = X / 5;
        }
 
        // If X is not divisible by
        // 2, 3, or 5, then print -1
        else {
            cout << "-1";
            return;
        }
 
        // Increment count by 1
        count++;
    }
 
    // Print the value of count as the
    // minimum number of operations
    cout << count;
}
 
// Driver Code
int main()
{
    int X = 15, Y = 20;
    minimumOperations(X, Y);
 
    return 0;
}

Java




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to calculate
// GCD of two numbers
static int gcd(int a, int b)
{
     
    // Base Case
    if (b == 0)
    {
        return a;
    }
 
    // Calculate GCD recursively
    return gcd(b, a % b);
}
 
// Function to count the minimum
// number of divisions required
// to make X and Y equal
static void minimumOperations(int X, int Y)
{
     
    // Calculate GCD of X and Y
    int GCD = gcd(X, Y);
 
    // Divide X and Y by their GCD
    X = X / GCD;
    Y = Y / GCD;
 
    // Stores the number of divisions
    int count = 0;
 
    // Iterate until X != Y
    while (X != Y)
    {
         
        // Maintain the order X <= Y
        if (Y > X)
        {
            int t = X;
            X = Y;
            Y = t;
        }
 
        // If X is divisible by 2,
        // then divide X by 2
        if (X % 2 == 0)
        {
            X = X / 2;
        }
 
        // If X is divisible by 3,
        // then divide X by 3
        else if (X % 3 == 0)
        {
            X = X / 3;
        }
 
        // If X is divisible by 5,
        // then divide X by 5
        else if (X % 5 == 0)
        {
            X = X / 5;
        }
 
        // If X is not divisible by
        // 2, 3, or 5, then print -1
        else
        {
            System.out.print("-1");
            return;
        }
 
        // Increment count by 1
        count += 1;
    }
 
    // Print the value of count as the
    // minimum number of operations
    System.out.println(count);
}
 
// Driver Code
static public void main(String args[])
{
    int X = 15, Y = 20;
     
    minimumOperations(X, Y);
}
}
 
// This code is contributed by ipg2016107

Python3




# Python3 program for the above approach
 
# Function to calculate
# GCD of two numbers
def gcd(a, b):
     
    # Base Case
    if (b == 0):
        return a
 
    # Calculate GCD recursively
    return gcd(b, a % b)
 
# Function to count the minimum
# number of divisions required
# to make X and Y equal
def minimumOperations(X, Y):
     
    # Calculate GCD of X and Y
    GCD = gcd(X, Y)
 
    # Divide X and Y by their GCD
    X = X // GCD
    Y = Y // GCD
 
    # Stores the number of divisions
    count = 0
 
    # Iterate until X != Y
    while (X != Y):
 
        # Maintain the order X <= Y
        if (Y > X):
            X, Y = Y, X
 
        # If X is divisible by 2,
        # then divide X by 2
        if (X % 2 == 0):
            X = X // 2
 
        # If X is divisible by 3,
        # then divide X by 3
        elif (X % 3 == 0):
            X = X // 3
 
        # If X is divisible by 5,
        # then divide X by 5
        elif (X % 5 == 0):
            X = X // 5
             
        # If X is not divisible by
        # 2, 3, or 5, then pr-1
        else:
            print("-1")
            return
 
        # Increment count by 1
        count += 1
 
    # Print the value of count as the
    # minimum number of operations
    print (count)
 
# Driver Code
if __name__ == '__main__':
     
    X, Y = 15, 20
     
    minimumOperations(X, Y)
 
# This code is contributed by mohit kumar 29

C#




// C# program for the above approach
using System;
 
public class GFG
{
 
// Function to calculate
// GCD of two numbers
static int gcd(int a, int b)
{
   
    // Base Case
    if (b == 0) {
        return a;
    }
 
    // Calculate GCD recursively
    return gcd(b, a % b);
}
 
// Function to count the minimum
// number of divisions required
// to make X and Y equal
static void minimumOperations(int X, int Y)
{
   
    // Calculate GCD of X and Y
    int GCD = gcd(X, Y);
 
    // Divide X and Y by their GCD
    X = X / GCD;
    Y = Y / GCD;
 
    // Stores the number of divisions
    int count = 0;
 
    // Iterate until X != Y
    while (X != Y) {
 
        // Maintain the order X <= Y
        if (Y > X) {
            int t = X;
            X = Y;
            Y = t;
        }
 
        // If X is divisible by 2,
        // then divide X by 2
        if (X % 2 == 0) {
            X = X / 2;
        }
 
        // If X is divisible by 3,
        // then divide X by 3
        else if (X % 3 == 0) {
            X = X / 3;
        }
 
        // If X is divisible by 5,
        // then divide X by 5
        else if (X % 5 == 0) {
            X = X / 5;
        }
 
        // If X is not divisible by
        // 2, 3, or 5, then print -1
        else {
            Console.WriteLine("-1");
            return;
        }
 
        // Increment count by 1
        count++;
    }
 
    // Print the value of count as the
    // minimum number of operations
    Console.WriteLine(count);
}
 
// Driver Code
static public void Main()
{
    int X = 15, Y = 20;
    minimumOperations(X, Y);
}
}
 
// This code is contributed by sanjoy_62.

Javascript




<script>
 
// Javascript program for the above approach
 
    // Function to calculate
    // GCD of two numbers
    function gcd(a , b) {
 
        // Base Case
        if (b == 0) {
            return a;
        }
 
        // Calculate GCD recursively
        return gcd(b, a % b);
    }
 
    // Function to count the minimum
    // number of divisions required
    // to make X and Y equal
    function minimumOperations(X , Y) {
 
        // Calculate GCD of X and Y
        var GCD = gcd(X, Y);
 
        // Divide X and Y by their GCD
        X = X / GCD;
        Y = Y / GCD;
 
        // Stores the number of divisions
        var count = 0;
 
        // Iterate until X != Y
        while (X != Y) {
 
            // Maintain the order X <= Y
            if (Y > X) {
                var t = X;
                X = Y;
                Y = t;
            }
 
            // If X is divisible by 2,
            // then divide X by 2
            if (X % 2 == 0) {
                X = X / 2;
            }
 
            // If X is divisible by 3,
            // then divide X by 3
            else if (X % 3 == 0) {
                X = X / 3;
            }
 
            // If X is divisible by 5,
            // then divide X by 5
            else if (X % 5 == 0) {
                X = X / 5;
            }
 
            // If X is not divisible by
            // 2, 3, or 5, then prvar -1
            else {
                document.write("-1");
                return;
            }
 
            // Increment count by 1
            count += 1;
        }
 
        // Print the value of count as the
        // minimum number of operations
        document.write(count);
    }
 
    // Driver Code
        var X = 15, Y = 20;
 
        minimumOperations(X, Y);
 
// This code contributed by Rajput-Ji
 
</script>
Output: 
3

 

Time Complexity: O(log(max(X, Y)))
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :