Open in App
Not now

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

 `// C++ code to implement the approach`   `#include ` `using` `namespace` `std;`   `// User defined function for checking if` `// point lies in square spiral path or not` `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()` `{` `    ``// Input values of X and Y coordinates` `    ``int` `X = -2, Y = -2;`   `    ``// Function call` `    ``cout << PointLies(X, Y);` `}`

## Java

 `// Java code to implement the approach`   `import` `java.io.*;` `import` `java.lang.*;` `import` `java.util.*;`   `class` `GFG {`   `    ``// Driver code` `    ``public` `static` `void` `main(String[] args)` `        ``throws` `java.lang.Exception` `    ``{` `        ``// Input values of X and Y coordinates` `        ``int` `X = -``2``, Y = -``2``;`   `        ``// Function call` `        ``System.out.println(PointLies(X, Y));` `    ``}`   `    ``// User defined function for checking if` `    ``// point lies in square spiral path or not` `    ``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"`     `# Input values of X and Y coordinates` `X ``=` `-``2` `Y ``=` `-``2`   `# Function call` `print``(PointLies(X, Y))`

## C#

 `using` `System;`   `public` `class` `GFG {`   `    ``static` `void` `Main(``string``[] args)` `    ``{` `        ``// Input values of X and Y coordinates` `        ``int` `X = -2, Y = -2;`   `        ``// Function call` `        ``Console.WriteLine(PointLies(X, Y));` `    ``}`   `    ``// User defined function for checking if` `    ``// point lies in square spiral path or not` `    ``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

 `// JavaScript code for the above approach`   `// User defined function for checking if` `// point lies in square spiral path or not` `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"``;` `}`   `// Input values of X and Y coordinates` `var` `X = -2, Y = -2;`   `// Function call` `console.log(PointLies(X, Y));`   `// This code is contributed by divya_p123.`

Output

`YES`

Time Complexity: O(1)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Related Articles