Check if it is possible to reach (X, Y) from origin such that in each ith move increment x or y coordinate with 3^i
Given two positive integers X and Y, the task is to find whether a point (X, Y) can be reached from the point (0, 0) such that in each ith move x-coordinate or y-coordinate can be incremented by 3i. If it is possible then print Yes. Otherwise, print No.
Examples:
Input: X = 1, Y = 3
Output: Yes
Explanation:
Following are the steps from (0, 0) to (1, 3):
Step 0: Increment the X coordinate by 30(= 1) modifies the coordinates to (1, 0).
Step 1: Increment the Y coordinate by 31(= 2) modifies the coordinates to (1, 3).
Therefore, the coordinates (1, 3) can be reached from (0, 0). Hence, print Yes.
Input: X = 10, Y = 30
Output: Yes
Naive Approach: The simplest approach to solve the given problem is to generate all possible moves from (X, Y) by decrementing 3i in each ith steps and check if any such combinations of moves reach (0, 0) or not. If it is possible then print Yes. Otherwise, print No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool canReach( int X, int Y, int steps)
{
if (X == 0 && Y == 0) {
return true ;
}
if (X < 0 || Y < 0) {
return false ;
}
return (
canReach(X - ( int ) pow (3, steps),
Y, steps + 1)
| canReach(X, Y - ( int ) pow (3, steps),
steps + 1));
}
int main()
{
int X = 10, Y = 30;
if (canReach(X, Y, 0)) {
cout << "YES" << endl;
}
else
cout << "NO" << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static boolean canReach( int X, int Y, int steps)
{
if (X == 0 && Y == 0 ) {
return true ;
}
if (X < 0 || Y < 0 ) {
return false ;
}
return (
canReach(X - ( int )Math.pow( 3 , steps),
Y, steps + 1 )
| canReach(X, Y - ( int )Math.pow( 3 , steps),
steps + 1 ));
}
public static void main(String[] args)
{
int X = 10 , Y = 30 ;
if (canReach(X, Y, 0 )) {
System.out.print( "YES" + "\n" );
}
else
System.out.print( "NO" + "\n" );
}
}
|
Python3
def canReach(X, Y, steps):
if (X = = 0 and Y = = 0 ):
return True
if (X < 0 or Y < 0 ):
return False
return (canReach(X - int ( pow ( 3 , steps)),Y, steps + 1 )| canReach(X, Y - int ( pow ( 3 , steps)),steps + 1 ))
if __name__ = = '__main__' :
X = 10
Y = 30
if (canReach(X, Y, 0 )):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static Boolean canReach( int X, int Y, int steps)
{
if (X == 0 && Y == 0) {
return true ;
}
if (X < 0 || Y < 0) {
return false ;
}
return (
canReach(X - ( int )Math.Pow(3, steps),
Y, steps + 1)
| canReach(X, Y - ( int )Math.Pow(3, steps),
steps + 1));
}
public static void Main(String[] args)
{
int X = 10, Y = 30;
if (canReach(X, Y, 0)) {
Console.Write( "YES" + "\n" );
}
else
Console.Write( "NO" + "\n" );
}
}
|
Javascript
<script>
function canReach(X, Y, steps) {
if (X == 0 && Y == 0) {
return true ;
}
if (X < 0 || Y < 0) {
return false ;
}
return (
canReach(X - Math.pow(3, steps), Y, steps + 1) |
canReach(X, Y - Math.pow(3, steps), steps + 1)
);
}
let X = 10,
Y = 30;
if (canReach(X, Y, 0)) {
document.write( "YES" );
} else document.write( "NO" );
</script>
|
Time Complexity: O(2K), where K is the maximum number of steps performed.
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observations by converting X and Y into base 3:
- If there is 1 in both the value of X and Y in base 3, then (X, Y) can’t be reached as this step can’t be performed in both directions.
- If there is 2 in any value of X and Y in base 3, then (X, Y) can’t be reached as this can’t be represented in the perfect power of 3.
- If there is 0 in any value of X and Y in base 3, then (X, Y) can’t be reached as this step can’t be performed except the last step.
- Otherwise, in all the remaining cases (X, Y) can be reached from (0, 0).
From the above observations, print the result accordingly.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool canReach( int X, int Y)
{
int steps = 0;
while (X || Y) {
int pos1 = X % 3;
int pos2 = Y % 3;
if (pos1 == 2 || pos2 == 2) {
return false ;
}
if (pos1 == 1 && pos2 == 1) {
return false ;
}
if (pos1 == 0 && pos2 == 0) {
return false ;
}
X /= 3;
Y /= 3;
steps++;
}
return true ;
}
int main()
{
int X = 10, Y = 30;
if (canReach(X, Y)) {
cout << "YES" ;
}
else {
cout << "NO" ;
}
}
|
Java
import java.io.*;
class GFG
{
static boolean canReach( int X, int Y)
{
int steps = 0 ;
while (X != 0 || Y != 0 ) {
int pos1 = X % 3 ;
int pos2 = Y % 3 ;
if (pos1 == 2 || pos2 == 2 ) {
return false ;
}
if (pos1 == 1 && pos2 == 1 ) {
return false ;
}
if (pos1 == 0 && pos2 == 0 ) {
return false ;
}
X /= 3 ;
Y /= 3 ;
steps++;
}
return true ;
}
public static void main(String[] args)
{
int X = 10 , Y = 30 ;
if (canReach(X, Y)) {
System.out.println( "YES" );
}
else {
System.out.println( "NO" );
}
}
}
|
Python3
def canReach(X, Y):
steps = 0
while (X ! = 0 or Y ! = 0 ):
pos1 = X % 3
pos2 = Y % 3
if (pos1 = = 2 or pos2 = = 2 ):
return False
if (pos1 = = 1 and pos2 = = 1 ):
return False
if (pos1 = = 0 and pos2 = = 0 ):
return False
X / = 3
Y / = 3
steps + = 1
return True
X = 10
Y = 30
if (canReach(X, Y)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public class GFG
{
static bool canReach( int X, int Y)
{
int steps = 0;
while (X != 0 || Y != 0) {
int pos1 = X % 3;
int pos2 = Y % 3;
if (pos1 == 2 || pos2 == 2) {
return false ;
}
if (pos1 == 1 && pos2 == 1) {
return false ;
}
if (pos1 == 0 && pos2 == 0) {
return false ;
}
X /= 3;
Y /= 3;
steps++;
}
return true ;
}
public static void Main(String[] args)
{
int X = 10, Y = 30;
if (canReach(X, Y)) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
}
|
Javascript
<script>
function canReach(X , Y)
{
var steps = 0;
while (X != 0 || Y != 0) {
var pos1 = X % 3;
var pos2 = Y % 3;
if (pos1 == 2 || pos2 == 2) {
return false ;
}
if (pos1 == 1 && pos2 == 1) {
return false ;
}
if (pos1 == 0 && pos2 == 0) {
return false ;
}
X /= 3;
Y /= 3;
steps++;
}
return true ;
}
var X = 10, Y = 30;
if (canReach(X, Y)) {
document.write( "YES" );
}
else {
document.write( "NO" );
}
</script>
|
Time Complexity: O(log3(max(X, Y))
Auxiliary Space: O(1)
Last Updated :
14 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...