Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Difficulty Level : Hard
  • Last Updated : 14 Sep, 2021

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:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find whether (0, 0) can
// be reached from (X, Y) by decrementing
// 3^i at each ith step
bool canReach(int X, int Y, int steps)
{
    // Termination Condition
    if (X == 0 && Y == 0) {
        return true;
    }
 
    if (X < 0 || Y < 0) {
        return false;
    }
 
    // Otherwise, recursively call by
    // decrementing 3^i at each step
    return (
        canReach(X - (int)pow(3, steps),
                 Y, steps + 1)
        | canReach(X, Y - (int)pow(3, steps),
                   steps + 1));
}
 
// Driver Code
int main()
{
    int X = 10, Y = 30;
    if (canReach(X, Y, 0)) {
        cout << "YES" << endl;
    }
    else
        cout << "NO" << endl;
    return 0;
}

Java




// Java program for the above approach
import java.util.*;
 
class GFG
{
 
// Function to find whether (0, 0) can
// be reached from (X, Y) by decrementing
// 3^i at each ith step
static boolean canReach(int X, int Y, int steps)
{
   
    // Termination Condition
    if (X == 0 && Y == 0) {
        return true;
    }
 
    if (X < 0 || Y < 0) {
        return false;
    }
 
    // Otherwise, recursively call by
    // decrementing 3^i at each step
    return (
        canReach(X - (int)Math.pow(3, steps),
                 Y, steps + 1)
        | canReach(X, Y - (int)Math.pow(3, steps),
                   steps + 1));
}
 
// Driver Code
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");
}
}
 
// This code is contributed by Amit Katiyar

Python3




# Python 3 program for the above approach
 
# Function to find whether (0, 0) can
# be reached from (X, Y) by decrementing
# 3^i at each ith step
def canReach(X, Y, steps):
    # Termination Condition
    if (X == 0 and Y == 0):
        return True
 
    if (X < 0 or Y < 0):
        return False
 
    # Otherwise, recursively call by
    # decrementing 3^i at each step
    return (canReach(X - int(pow(3, steps)),Y, steps + 1)| canReach(X, Y - int(pow(3, steps)),steps + 1))
 
# Driver Code
if __name__ == '__main__':
    X = 10
    Y = 30
    if(canReach(X, Y, 0)):
        print("YES")
    else:
        print("NO")
         
        # This code is contributed by SURENDRA_GANGWAR.

C#




// C# program for the above approach
using System;
 
class GFG
{
 
// Function to find whether (0, 0) can
// be reached from (X, Y) by decrementing
// 3^i at each ith step
static Boolean canReach(int X, int Y, int steps)
{
   
    // Termination Condition
    if (X == 0 && Y == 0) {
        return true;
    }
 
    if (X < 0 || Y < 0) {
        return false;
    }
 
    // Otherwise, recursively call by
    // decrementing 3^i at each step
    return (
        canReach(X - (int)Math.Pow(3, steps),
                 Y, steps + 1)
        | canReach(X, Y - (int)Math.Pow(3, steps),
                   steps + 1));
}
 
// Driver Code
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");
}
}
 
// This code is contributed by shivanisinghss2110

Javascript




<script>
// Javascript program for the above approach
 
// Function to find whether (0, 0) can
// be reached from (X, Y) by decrementing
// 3^i at each ith step
function canReach(X, Y, steps) {
  // Termination Condition
  if (X == 0 && Y == 0) {
    return true;
  }
 
  if (X < 0 || Y < 0) {
    return false;
  }
 
  // Otherwise, recursively call by
  // decrementing 3^i at each step
  return (
    canReach(X - Math.pow(3, steps), Y, steps + 1) |
    canReach(X, Y - Math.pow(3, steps), steps + 1)
  );
}
 
// Driver Code
 
let X = 10,
  Y = 30;
if (canReach(X, Y, 0)) {
  document.write("YES");
} else document.write("NO");
 
// This code is contributed by gfgking.
</script>
Output: 
YES

 

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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find whether (0, 0) can
// be reached from (X, Y) by decrementing
// 3^i at each ith step
bool canReach(int X, int Y)
{
    // Stores the number of steps
    // performed to reach (X, Y)
    int steps = 0;
    while (X || Y) {
 
        // Value of X in base 3
        int pos1 = X % 3;
 
        // Value of Y in base 3
        int pos2 = Y % 3;
 
        // Check if any has value 2
        if (pos1 == 2 || pos2 == 2) {
            return false;
        }
 
        // If both have value 1
        if (pos1 == 1 && pos2 == 1) {
            return false;
        }
 
        // If both have value 0
        if (pos1 == 0 && pos2 == 0) {
            return false;
        }
 
        X /= 3;
        Y /= 3;
        steps++;
    }
 
    // Otherwise, return true
    return true;
}
 
// Driver Code
int main()
{
    int X = 10, Y = 30;
    if (canReach(X, Y)) {
        cout << "YES";
    }
    else {
        cout << "NO";
    }
}

Java




// Java program for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to find whether (0, 0) can
    // be reached from (X, Y) by decrementing
    // 3^i at each ith step
    static boolean canReach(int X, int Y)
    {
       
        // Stores the number of steps
        // performed to reach (X, Y)
        int steps = 0;
        while (X != 0 || Y != 0) {
 
            // Value of X in base 3
            int pos1 = X % 3;
 
            // Value of Y in base 3
            int pos2 = Y % 3;
 
            // Check if any has value 2
            if (pos1 == 2 || pos2 == 2) {
                return false;
            }
 
            // If both have value 1
            if (pos1 == 1 && pos2 == 1) {
                return false;
            }
 
            // If both have value 0
            if (pos1 == 0 && pos2 == 0) {
                return false;
            }
 
            X /= 3;
            Y /= 3;
            steps++;
        }
 
        // Otherwise, return true
        return true;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int X = 10, Y = 30;
        if (canReach(X, Y)) {
            System.out.println("YES");
        }
        else {
            System.out.println("NO");
        }
    }
}
 
// This code is contributed by Potta Lokesh

Python3




# Python program for the above approach
 
# Function to find whether (0, 0) can
# be reached from (X, Y) by decrementing
# 3^i at each ith step
def canReach(X, Y):
 
    # Stores the number of steps
    # performed to reach (X, Y)
    steps = 0
    while (X != 0 or Y != 0):
 
        # Value of X in base 3
        pos1 = X % 3
 
        # Value of Y in base 3
        pos2 = Y % 3
 
        # Check if any has value 2
        if (pos1 == 2 or pos2 == 2):
            return False
 
        # If both have value 1
        if (pos1 == 1 and pos2 == 1):
            return False
 
        # If both have value 0
        if (pos1 == 0 and pos2 == 0):
            return False
 
        X /= 3
        Y /= 3
        steps += 1
 
    # Otherwise, return true
    return True
 
# Driver Code
X = 10
Y = 30
if (canReach(X, Y)):
    print("YES")
else:
    print("NO")
 
# This code is contributed by _saurabh_jaiswal

C#




// C# program for the above approach
using System;
 
public class GFG
{
   
    // Function to find whether (0, 0) can
    // be reached from (X, Y) by decrementing
    // 3^i at each ith step
    static bool canReach(int X, int Y)
    {
       
        // Stores the number of steps
        // performed to reach (X, Y)
        int steps = 0;
        while (X != 0 || Y != 0) {
 
            // Value of X in base 3
            int pos1 = X % 3;
 
            // Value of Y in base 3
            int pos2 = Y % 3;
 
            // Check if any has value 2
            if (pos1 == 2 || pos2 == 2) {
                return false;
            }
 
            // If both have value 1
            if (pos1 == 1 && pos2 == 1) {
                return false;
            }
 
            // If both have value 0
            if (pos1 == 0 && pos2 == 0) {
                return false;
            }
 
            X /= 3;
            Y /= 3;
            steps++;
        }
 
        // Otherwise, return true
        return true;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int X = 10, Y = 30;
        if (canReach(X, Y)) {
            Console.WriteLine("YES");
        }
        else {
            Console.WriteLine("NO");
        }
    }
}
 
// This code is contributed by Princi Singh

Javascript




<script>
// javascript program for the above approach
  
    // Function to find whether (0, 0) can
    // be reached from (X, Y) by decrementing
    // 3^i at each ith step
    function canReach(X , Y)
    {
       
        // Stores the number of steps
        // performed to reach (X, Y)
        var steps = 0;
        while (X != 0 || Y != 0) {
 
            // Value of X in base 3
            var pos1 = X % 3;
 
            // Value of Y in base 3
            var pos2 = Y % 3;
 
            // Check if any has value 2
            if (pos1 == 2 || pos2 == 2) {
                return false;
            }
 
            // If both have value 1
            if (pos1 == 1 && pos2 == 1) {
                return false;
            }
 
            // If both have value 0
            if (pos1 == 0 && pos2 == 0) {
                return false;
            }
 
            X /= 3;
            Y /= 3;
            steps++;
        }
 
        // Otherwise, return true
        return true;
    }
 
    // Driver Code
      var X = 10, Y = 30;
        if (canReach(X, Y)) {
            document.write("YES");
        }
        else {
            document.write("NO");
        }
 
// This code contributed by shikhasingrajput
</script>
Output: 
YES

 

Time Complexity: O(log3(max(X, Y))
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!