Make two numbers equal in at most Okay steps dividing by their issue


View Dialogue

Enhance Article

Save Article

Like Article

Given integers X, Y, and Okay, the duty is to make X and Y equal in no more than Okay operations by making use of the next operations:

  • Select an integer A and make X = X/A, (the place A is an integer that’s higher than 1 and never equal to X).
  • Select an integer A and make Y = Y/A, (the place A is an integer that’s higher than 1 and never equal to Y).

Examples:

Enter: X = 10, Y = 20, Okay = 4
Output: YES
Rationalization: One optimum resolution to make them equal is:
First select A = 2 and do X = X/2 so now X is the same as 5.
Now select A = 4 and do Y = Y/4 so now Y is the same as 5.
Since we’ve got utilized solely two operations right here which is lower than Okay 
to make X and Y equal and in addition higher than one.
Due to this fact the reply is YES.

Enter: X = 2, Y = 27, Okay = 1
Output: NO
Rationalization: There isn’t any potential strategy to make X and Y equal 
in lower than or equal to 1 Operation.

 

Method: To resolve the issue observe the under thought:

Listed below are solely two circumstances potential:

  • When Okay is the same as one and 
  • When Okay is larger than 1

If Okay is the same as one then it’s potential to make X and Y equal solely when both X is divisible by Y or Y is divisible by X

If Okay is larger than 1 then X and Y will be equal and higher than 1 solely when their GCD is larger than 1.

Observe the steps talked about under to implement the thought:

  • Test if Okay = 1:
    • In that case, discover out if any of them is a divisor of the opposite.
  • In any other case, discover the GCD of the numbers.
    • If the GCD is 1 then it’s not potential.
    • In any other case, a solution at all times exists.
See also  How Capital One is strengthening the software program provide chain

Under is the implementation for the above method:

C++

  

#embody <bits/stdc++.h>

utilizing namespace std;

  

bool isPossible(int X, int Y, int Okay)

{

    

    if (Okay == 1) Y % X == 0)

            return true;

        return false;

    

  

    

    else {

        if (__gcd(X, Y) > 1)

            return true;

        return false;

    }

    return false;

}

  

int important()

{

    int X = 10;

    int Y = 20;

    int Okay = 4;

  

    

    bool reply = isPossible(X, Y, Okay);

    if (reply)

        cout << "YES";

    else

        cout << "NO";

    return 0;

}

Time Complexity: O(1)
Auxiliary House: O(1)

Leave a Reply