Check if given point lies on the Square Spiral Path
Last Updated :
24 Jan, 2023
Given a 2D Graph, present initially at (0, 0). Print “YES” if the given point (X, Y) lies on the spiral path, otherwise “NO“. The spiral path is to be followed ln a sequential manner as given below:
- Move one unit right side from the current position.
- Move two units upwards from the current position.
- Move three units left side from the current position.
- Move four units downwards and so on.
Examples:
Input: X = 0, Y = 0
Output: YES
Explanation:
Graphical Explanation of input case 1
The point (X, Y) = (0, 0) denotes the origin. The point is at the path. Therefore, the output is YES.
Input: X = -2, Y = -2
Output: YES
Explanation:
Graphical Explanation of input case 2
It can be verified that the point lies on the square spiral path.
Input: X = 1, Y = -1
Output: NO
Explanation: It can be verified that the given point doesn’t lies on the square spiral path.
Approach: This can be solved using the following idea:
The problem is observation based and can be solved by using some mathematical observation. For solving the problem there are some conditions at which the problem has a solution:
- y ? 0, y % 2 = 0, x ? y and x ? (-1 * y) + 1
- x > 0, x % 2 = 1, y ? x + 1 and y ? (-1 * x) + 1
- y > 0, y % 2 = 0, x ? (-1 * y) and x < y
- x < 0, x % 2 = 0, y ? x and y ? (-1 * x)
All rest of the conditions will not have any solution.
Follow the steps mentioned below to implement the idea:
- Check for the conditions mentioned above
- If any one of the conditions is satisfied, then the point lies in the path.
- Else the point lies outside the path.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string PointLies( int x, int y)
{
if (y <= 0 && y % 2 == 0 && x >= y
&& x <= (-1 * y) + 1) {
return "YES" ;
}
else if (x > 0 && x % 2 == 1 && y <= x + 1
&& y >= (-1 * x) + 1) {
return "YES" ;
}
else if (y > 0 && y % 2 == 0 && x >= (-1 * y)
&& x < y) {
return "YES" ;
}
else if (x < 0 && x % 2 == 0 && y >= x
&& y <= (-1 * x)) {
return "YES" ;
}
return "NO" ;
}
int main()
{
int X = -2, Y = -2;
cout << PointLies(X, Y);
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
int X = - 2 , Y = - 2 ;
System.out.println(PointLies(X, Y));
}
static String PointLies( int x, int y)
{
if (y <= 0 && y % 2 == 0 && x >= y
&& x <= (- 1 * y) + 1 ) {
return "YES" ;
}
else if (x > 0 && x % 2 == 1 && y <= x + 1
&& y >= (- 1 * x) + 1 ) {
return "YES" ;
}
else if (y > 0 && y % 2 == 0 && x >= (- 1 * y)
&& x < y) {
return "YES" ;
}
else if (x < 0 && x % 2 == 0 && y >= x
&& y <= (- 1 * x)) {
return "YES" ;
}
return "NO" ;
}
}
|
Python3
def PointLies(x, y):
if y < = 0 and y % 2 = = 0 and x > = y and x < = ( - 1 * y) + 1 :
return "YES"
elif x > 0 and x % 2 = = 1 and y < = x + 1 and y > = ( - 1 * x) + 1 :
return "YES"
elif y > 0 and y % 2 = = 0 and x > = ( - 1 * y) and x < y:
return "YES"
elif x < 0 and x % 2 = = 0 and y > = x and y < = ( - 1 * x):
return "YES"
return "NO"
X = - 2
Y = - 2
print (PointLies(X, Y))
|
C#
using System;
public class GFG {
static void Main( string [] args)
{
int X = -2, Y = -2;
Console.WriteLine(PointLies(X, Y));
}
static string PointLies( int x, int y)
{
if (y <= 0 && y % 2 == 0 && x >= y
&& x <= (-1 * y) + 1) {
return "YES" ;
}
else if (x > 0 && x % 2 == 1 && y <= x + 1
&& y >= (-1 * x) + 1) {
return "YES" ;
}
else if (y > 0 && y % 2 == 0 && x >= (-1 * y)
&& x < y) {
return "YES" ;
}
else if (x < 0 && x % 2 == 0 && y >= x
&& y <= (-1 * x)) {
return "YES" ;
}
return "NO" ;
}
}
|
Javascript
function PointLies(x, y) {
if (y <= 0 && y % 2 === 0 && x >= y && x <= (-1 * y) + 1) {
return "YES" ;
} else if (x > 0 && x % 2 === 1 && y <= x + 1 && y >= (-1 * x) + 1) {
return "YES" ;
} else if (y > 0 && y % 2 === 0 && x >= (-1 * y) && x < y) {
return "YES" ;
} else if (x < 0 && x % 2 === 0 && y >= x && y <= (-1 * x)) {
return "YES" ;
}
return "NO" ;
}
var X = -2, Y = -2;
console.log(PointLies(X, Y));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...