Make two numbers equal in at most K steps dividing by their factor
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++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to check the possibility of the answer bool isPossible( int X, int Y, int K) { // Case 1: when k=1 if (K == 1) { if (X % Y == 0 || Y % X == 0) return true ; return false ; } // Case 2: when k>1 else { if (__gcd(X, Y) > 1) return true ; return false ; } return false ; } // Driver code int main() { int X = 10; int Y = 20; int K = 4; // Function call bool answer = isPossible(X, Y, K); if (answer) cout << "YES" ; else cout << "NO" ; return 0; } |
Java
// Java code to implement the approach public class GFG { // Recursive function to return gcd of a and b static int gcd( int a, int b) { // Everything divides 0 if (a == 0 ) return b; if (b == 0 ) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } // Function to check the possibility of the answer static boolean isPossible( int X, int Y, int K) { // Case 1: when k=1 if (K == 1 ) { if (X % Y == 0 || Y % X == 0 ) return true ; return false ; } // Case 2: when k>1 else { if (gcd(X, Y) > 1 ) return true ; return false ; } } // Driver code public static void main (String[] args) { int X = 10 ; int Y = 20 ; int K = 4 ; // Function call boolean answer = isPossible(X, Y, K); if (answer == true ) System.out.println( "YES" ); else System.out.println( "NO" ); } } // This code is contributed by AnkThon |
Python3
# Python3 code to implement the approach import math # Function to check the possibility of the answer def isPossible(X, Y, K): # Case 1: when k=1 if (K = = 1 ) : return (X % Y = = 0 or Y % X = = 0 ) # Case 2: when k>1 else : return (math.gcd(X, Y) > 1 ) return False # Driver code X, Y, K = 10 , 20 , 4 # Function call answer = isPossible(X, Y, K); if (answer): print ( "YES" ) else : print ( "NO" ) # This code is contributed by phasing17 |
C#
// C# code to implement the approach using System; public class GFG { // Recursive function to return gcd of a and b static int gcd( int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return gcd(a-b, b); return gcd(a, b-a); } // Function to check the possibility of the answer static bool isPossible( int X, int Y, int K) { // Case 1: when k=1 if (K == 1) { if (X % Y == 0 || Y % X == 0) return true ; return false ; } // Case 2: when k>1 else { if (gcd(X, Y) > 1) return true ; return false ; } } // Driver code public static void Main ( string [] args) { int X = 10; int Y = 20; int K = 4; // Function call bool answer = isPossible(X, Y, K); if (answer == true ) Console.WriteLine( "YES" ); else Console.WriteLine( "NO" ); } } // This code is contributed by AnkThon |
Javascript
<script> //Javascript code to implement the approach function _gcd(a,b){ if (b==0) return a; return _gcd(b,a%b); } // Function to check the possibility of the answer function isPossible(X, Y, K) { // Case 1: when k=1 if (K == 1) { if (X % Y == 0 || Y % X == 0) return true ; return false ; } // Case 2: when k>1 else { if (_gcd(X, Y) > 1) return true ; return false ; } return false ; } // Driver code let X = 10; let Y = 20; let K = 4; // Function call let answer = isPossible(X, Y, K); if (answer) document.write( "YES" , "</br>" ) else document.write( "NO" , "</br>" ) // This code is contributed by satwik4409. </script> |
YES
Time Complexity: O(1)
Auxiliary Space: O(1)
Please Login to comment...