We are given two numbers A and B, we need to write a program to determine if A and B can be reached starting from (1, 1) following the given steps. Start from (1, 1) and at every step choose a random number K and multiply K to any one of the two numbers obtained in the previous step and K2 to the other number.
Examples:
Input: A = 3, B = 9
Output: yes
Explanation: Starting from A = 1 and B = 1.
We choose k=3 and multiply 3 with the first number to get A=3 and multiply k2=9 to the second-number to get B=9.
Input: A = 60, B = 450
Output: yes
Explanation : Starting from A = 1 and B = 1,
Step 1: multiply k=3 and k2 to get 3 and 9
Step 2: Multiply k=5 and k2 = 25 to get to 15 and 225
Step 3: Multiply k2=4 and k=2 to get to A=60 and B=450
The idea to solve this problem is to observe closely that at each step we are multiplying k and k2 to the numbers. So if A and B can be reached, it will have k^3 at every step as factors in A*B. In simple words, if the number A*B is a perfect cube and it divides A and B both, only then the number can be reached starting from 1 and 1 by performing given steps.
Below is the implementation of above idea:
C++
#include <bits/stdc++.h>
using namespace std;
bool possibleToReach( int a, int b)
{
int c = cbrt(a * b);
int re1 = a / c;
int re2 = b / c;
if ((re1 * re1 * re2 == a) && (re2 * re2 * re1 == b))
return true ;
else
return false ;
}
int main()
{
int A = 60, B = 450;
if (possibleToReach(A, B))
cout << "yes" ;
else
cout << "no" ;
return 0;
}
|
Java
class GFG {
static boolean possibleToReach( int a, int b)
{
int c = ( int )Math.cbrt(a * b);
int re1 = a / c;
int re2 = b / c;
if ((re1 * re1 * re2 == a) &&
(re2 * re2 * re1 == b))
return true ;
else
return false ;
}
public static void main(String[] args)
{
int A = 60 , B = 450 ;
if (possibleToReach(A, B))
System.out.println( "yes" );
else
System.out.println( "no" );
}
}
|
Python 3
import numpy as np
def possibleToReach(a, b):
c = np.cbrt(a * b)
re1 = a / / c
re2 = b / / c
if ((re1 * re1 * re2 = = a) and
(re2 * re2 * re1 = = b)):
return True
else :
return False
if __name__ = = "__main__" :
A = 60
B = 450
if (possibleToReach(A, B)):
print ( "yes" )
else :
print ( "no" )
|
C#
using System;
public class GFG{
public static bool possibleToReach( int a, int b)
{
int c = ( int )Math.Pow(a * b, ( double ) 1 / 3);
int re1 = a / c;
int re2 = b / c;
if ((re1 * re1 * re2 == a) &&
(re2 * re2 * re1 == b))
return true ;
else
return false ;
}
static public void Main (String []args)
{
int A = 60, B = 450;
if (possibleToReach(A, B))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function possibleToReach( $a , $b )
{
$c =( $a * $b );
$re1 = $a / $c ;
$re2 = $b / $c ;
if (( $re1 * $re1 * $re2 == $a ) &&
( $re2 * $re2 * $re1 == $b ))
return 1;
else
return -1;
}
$A = 60;
$B = 450;
if (possibleToReach( $A , $B ))
echo "yes" ;
else
echo "no" ;
?>
|
Javascript
<script>
function possibleToReach(a, b)
{
let c = Math.cbrt(a * b);
let re1 = a / c;
let re2 = b / c;
if ((re1 * re1 * re2 == a) &&
(re2 * re2 * re1 == b))
return true ;
else
return false ;
}
let A = 60, B = 450;
if (possibleToReach(A, B))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time complexity: O(log(A*B)) for given A and B, as it is using cbrt function
Auxiliary space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
20 Feb, 2023
Like Article
Save Article