Make two numbers equal in at most K steps dividing by their factor
Last Updated :
21 Jul, 2022
Given integers X, Y, and K, the task is to make X and Y equal in not more than K operations by applying the following operations:
- Choose an integer A and make X = X/A, (where A is an integer that is greater than 1 and not equal to X).
- Choose an integer A and make Y = Y/A, (where A is an integer that is greater than 1 and not equal to Y).
Examples:
Input: X = 10, Y = 20, K = 4
Output: YES
Explanation: One optimal solution to make them equal is:
First choose A = 2 and do X = X/2 so now X is equal to 5.
Now choose A = 4 and do Y = Y/4 so now Y is equal to 5.
Since we have applied only two operations here which is less than K
to make X and Y equal and also greater than one.
Therefore the answer is YES.
Input: X = 2, Y = 27, K = 1
Output: NO
Explanation: There is no possible way to make X and Y equal
in less than or equal to 1 Operation.
Approach: To solve the problem follow the below idea:
Here are only two cases possible:
- When K is equal to one and
- When K is greater than 1
If K is equal to one then it is possible to make X and Y equal only when either X is divisible by Y or Y is divisible by X
If K is greater than 1 then X and Y can be equal and greater than 1 only when their GCD is greater than 1.
Follow the steps mentioned below to implement the idea:
- Check if K = 1:
- In that case, find out if any of them is a divisor of the other.
- Otherwise, find the GCD of the numbers.
- If the GCD is 1 then it is not possible.
- Otherwise, an answer always exists.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPossible( int X, int Y, int K)
{
if (K == 1) {
if (X % Y == 0 || Y % X == 0)
return true ;
return false ;
}
else {
if (__gcd(X, Y) > 1)
return true ;
return false ;
}
return false ;
}
int main()
{
int X = 10;
int Y = 20;
int K = 4;
bool answer = isPossible(X, Y, K);
if (answer)
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
public class GFG {
static int gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
static boolean isPossible( int X, int Y, int K)
{
if (K == 1 ) {
if (X % Y == 0 || Y % X == 0 )
return true ;
return false ;
}
else {
if (gcd(X, Y) > 1 )
return true ;
return false ;
}
}
public static void main (String[] args)
{
int X = 10 ;
int Y = 20 ;
int K = 4 ;
boolean answer = isPossible(X, Y, K);
if (answer == true )
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
import math
def isPossible(X, Y, K):
if (K = = 1 ) :
return (X % Y = = 0 or Y % X = = 0 )
else :
return (math.gcd(X, Y) > 1 )
return False
X, Y, K = 10 , 20 , 4
answer = isPossible(X, Y, K);
if (answer):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public class GFG {
static int gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
static bool isPossible( int X, int Y, int K)
{
if (K == 1) {
if (X % Y == 0 || Y % X == 0)
return true ;
return false ;
}
else {
if (gcd(X, Y) > 1)
return true ;
return false ;
}
}
public static void Main ( string [] args)
{
int X = 10;
int Y = 20;
int K = 4;
bool answer = isPossible(X, Y, K);
if (answer == true )
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
Javascript
<script>
function _gcd(a,b){
if (b==0)
return a;
return _gcd(b,a%b);
}
function isPossible(X, Y, K)
{
if (K == 1) {
if (X % Y == 0 || Y % X == 0)
return true ;
return false ;
}
else {
if (_gcd(X, Y) > 1)
return true ;
return false ;
}
return false ;
}
let X = 10;
let Y = 20;
let K = 4;
let answer = isPossible(X, Y, K);
if (answer)
document.write( "YES" , "</br>" )
else
document.write( "NO" , "</br>" )
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...