Reaching a point using clockwise or anticlockwise movements
Last Updated :
20 Feb, 2023
Given starting and ending position and a number N. Given that we are allowed to move in only four directions as shown in the image below. The directions of moves are U(\uparrow ), R\rightarrow , D\downarrow and L\leftarrow . We need to write a program to determine if starting from the given starting position we can reach the given end position in exactly N moves in moving about any direction(Clockwise or Anticlockwise).

Examples :
Input: start = U , end = L , N = 3
Output: Clockwise
Explanation: Step 1: move clockwise to reach R
Step 2: move clockwise to reach D
Step 3: move clockwise to reach L
So we reach from U to L in 3 steps moving in
clockwise direction.
Input: start = R , end = L , N = 3
Output: Not possible
Explanation: It is not possible to start from
R and end at L in 3 steps moving about in any
direction.
Input: start = D , end = R , N = 7
Output: Clockwise
Explanation: Starting at D, we complete one
complete clockwise round in 4 steps to reach D
again, then it takes 3 step to reach R
The idea to solve this problem is to observe that we can complete one round in 4 steps by traveling in any direction (clockwise or anti-clockwise), so taking n%4 steps is equivalent to taking n steps from the starting point. Therefore n is reduced to n%4. Consider the values of 'U' as 0, 'R' as 1, 'D' as 2 and 'L' as 3. If the abs(value(a)-value(b)) is 2 and n is also 2, then we can move either in clockwise or anticlockwise direction to reach the end position from the start position. If moving k steps in clockwise direction take us to the end position from start position then we can say that the condition for clockwise move will be (value(a)+k)%4==value(b). Similarly, the condition for anticlockwise move will be (value(a)+k*3)%4==value(b) since taking k step from position a in clockwise direction is equivalent to taking (a + k*3)%4 steps in anticlockwise direction.
Below is the implementation of the above approach:
C++
// CPP program to determine if
// starting from the starting
// position we can reach the
// end position in N moves
// moving about any direction
#include <bits/stdc++.h>
using namespace std;
// function that returns mark
// up value of directions
int value(char a)
{
if (a == 'U')
return 0;
if (a == 'R')
return 1;
if (a == 'D')
return 2;
if (a == 'L')
return 3;
}
// function to print
// the possible move
void printMove(char a, char b, int n)
{
// mod with 4 as completing
// 4 steps means completing
// one single round
n = n % 4;
// when n is 2 and the
// difference between moves is 2
if (n == 2 and abs(value(a) -
value(b)) == 2)
cout << "Clockwise or Anticlockwise";
// anticlockwise condition
else if ((value(a) + n * 3) % 4 == value(b))
cout << "Anticlockwise";
// clockwise condition
else if ((value(a) + n) % 4 == value(b))
cout << "Clockwise";
else
cout << "Not Possible";
}
// Driver Code
int main()
{
char a = 'D', b = 'R';
int n = 7;
printMove(a, b, n);
return 0;
}
Java
// Java program to determine if
// starting from the starting
// position we can reach the
// end position in N moves
// moving about any direction
class GFG
{
// function that returns mark
// up value of directions
static int value(char a)
{
if (a == 'U')
return 0;
if (a == 'R')
return 1;
if (a == 'D')
return 2;
if (a == 'L')
return 3;
return -1;
}
// function to print
// the possible move
static void printMove(char a,
char b,
int n)
{
// mod with 4 as completing
// 4 steps means completing
// one single round
n = n % 4;
// when n is 2 and
// the difference
// between moves is 2
if (n == 2 && Math.abs(value(a) -
value(b)) == 2)
System.out.println("Clockwise " +
" or Anticlockwise");
// anticlockwise condition
else if ((value(a) + n * 3) %
4 == value(b))
System.out.println("Anticlockwise");
// clockwise condition
else if ((value(a) + n) % 4 == value(b))
System.out.println("Clockwise");
else
System.out.println("Not Possible");
}
// Driver Code
public static void main(String args[])
{
char a = 'D', b = 'R';
int n = 7;
printMove(a, b, n);
}
}
// This code is contributed by Sam007
Python3
# python program to determine
# if starting from the starting
# position we can reach the end
# position in N moves moving
# any direction
# function that returns mark
# up value of directions
def value(a):
if (a == 'U'):
return 0
if (a == 'R'):
return 1
if (a == 'D'):
return 2
if (a == 'L'):
return 3
# function to print
# the possible move
def printMove(a, b, n):
# mod with 4 as completing
# 4 steps means completing
# one single round
n = n % 4;
# when n is 2 and
# the difference
# between moves is 2
if (n == 2 and
abs(value(a) - value(b)) == 2):
print ("Clockwise or Anticlockwise")
# anticlockwise condition
elif ((value(a) + n * 3) % 4 == value(b)):
print ("Anticlockwise")
# clockwise condition
elif ((value(a) + n) % 4 == value(b)):
print ("Clockwise")
else:
print ("Not Possible")
# Driver Code
a = 'D'
b = 'R'
n = 7
printMove(a, b, n)
# This code is contributed by Sam007.
C#
// C# program to determine
// if starting from the
// starting position we
// can reach the end position
// in N moves moving about
// any direction
using System;
class GFG
{
// function that returns mark
// up value of directions
static int value(char a)
{
if (a == 'U')
return 0;
if (a == 'R')
return 1;
if (a == 'D')
return 2;
if (a == 'L')
return 3;
return -1;
}
// function to print
// the possible move
static void printMove(char a,
char b,
int n)
{
// mod with 4 as completing
// 4 steps means completing
// one single round
n = n % 4;
// when n is 2 and
// the difference
// between moves is 2
if (n == 2 && Math.Abs(value(a) -
value(b)) == 2)
Console.Write("Clockwise " +
"or Anticlockwise");
// anticlockwise condition
else if ((value(a) + n * 3) %
4 == value(b))
Console.Write("Anticlockwise");
// clockwise condition
else if ((value(a) + n) %
4 == value(b))
Console.WriteLine("Clockwise");
else
Console.WriteLine("Not Possible");
}
// Driver Code
public static void Main()
{
char a = 'D', b = 'R';
int n = 7;
printMove(a, b, n);
}
}
// This code is contributed by Sam007
PHP
<?php
// PHP program to determine
// if starting from the
// starting position we can
// reach the end position in
// N moves moving about
// any direction
// function that returns mark
// up value of directions
function value($a)
{
if ($a == 'U')
return 0;
if ($a == 'R')
return 1;
if ($a == 'D')
return 2;
if ($a == 'L')
return 3;
}
// function to print
// the possible move
function printMove($a, $b,$n)
{
// mod with 4 as completing
// 4 steps means completing
// one single round
$n = $n % 4;
// when n is 2 and the
// difference between
// moves is 2
if ($n == 2 and abs(value($a) -
value($b)) == 2)
echo "Clockwise or Anticlockwise";
// anticlockwise condition
else if ((value($a) + $n * 3) %
4 == value($b))
echo "Anticlockwise";
// clockwise condition
else if ((value($a) + $n) %
4 == value($b))
echo "Clockwise";
else
echo "Not Possible";
}
// Driver Code
$a = 'D'; $b = 'R';
$n = 7;
printMove($a, $b, $n);
// This code is contributed ajit.
?>
JavaScript
<script>
// JavaScript program to determine if
// starting from the starting
// position we can reach the
// end position in N moves
// moving about any direction
// function that returns mark
// up value of directions
function value(a)
{
if (a == 'U')
return 0;
if (a == 'R')
return 1;
if (a == 'D')
return 2;
if (a == 'L')
return 3;
return -1;
}
// function to print
// the possible move
function printMove(a, b, n)
{
// mod with 4 as completing
// 4 steps means completing
// one single round
n = n % 4;
// when n is 2 and
// the difference
// between moves is 2
if (n == 2 && Math.abs(value(a) -
value(b)) == 2)
document.write("Clockwise " +
" or Anticlockwise");
// anticlockwise condition
else if ((value(a) + n * 3) %
4 == value(b))
document.write("Anticlockwise");
// clockwise condition
else if ((value(a) + n) % 4 == value(b))
document.write("Clockwise");
else
document.write("Not Possible");
}
// Driver code
let a = 'D', b = 'R';
let n = 7;
printMove(a, b, n);
// This code is contributed by code_hunt.
</script>
Output :
Clockwise
Time Complexity: O(1), as we are not using any loop or recursion to traverse.
Auxiliary Space: O(1), as we are not using any extra space.
Similar Reads
Restricted movement to reach the given point on a board Given a board of size 401x401 and 2 integers a and b. There is a person standing at position (0, 0), and the person has to reach (a, b) in minimum operations such that man is allowed to make only operations Left, Right, Top, Bottom, and Stand still. The person is not allowed to repeat the previous o
3 min read
Position of robot after given movements Given a robot which can only move in four directions, UP(U), DOWN(D), LEFT(L), RIGHT(R). Given a string consisting of instructions to move. Output the coordinates of a robot after executing the instructions. Initial position of robot is at origin(0, 0). Examples: Input : move = "UDDLRL" Output : (-1
6 min read
Print a singly linked list in spiral order Given a Linked list, the task is to print a singly linked list in a spiral fashion, starting from the mid and rotating clockwise. If the linked list has even nodes, then you have to choose the left mid. Examples: Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> XOutput: 3 -> 4 -> 2 ->
15+ min read
Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements Given a point (x, y), the task is to check if it is possible to reach from origin to (x, y) in exactly Z steps. From a given point (x, y) we can only moves in four direction left(x - 1, y), right(x + 1, y), up(x, y + 1) and down(x, y - 1).Examples: Input: x = 5, y = 5, z = 11 Output: Not PossibleInp
4 min read
Minimum moves to reach target on a infinite line | Set 2 Given a target position on the infinite number line, (-infinity to +infinity). Starting form 0 you have to reach the target by moving as described: In ith move, you can take i steps forward or backward. Find the minimum number of moves required to reach the target. Examples : Input : target = 3 Outp
6 min read
Counts paths from a point to reach Origin You are standing on a point (n, m) and you want to go to origin (0, 0) by taking steps either left or down i.e. from each point you are allowed to move either in (n-1, m) or (n, m-1). Find the number of paths from point to origin. Examples: Input : 3 6 Output : Number of Paths 84 Input : 3 0 Output
11 min read
Find Simple Closed Path for a given set of points Given a set of points, connect the dots without crossing. Example: Input: points[] = {(0, 3), (1, 1), (2, 2), (4, 4), (0, 0), (1, 2), (3, 1}, {3, 3}}; Output: Connecting points in following order would not cause any crossing {(0, 0), (3, 1), (1, 1), (2, 2), (3, 3), (4, 4), (1, 2), (0, 3)} We strongl
11 min read
Clockwise rotation of Linked List Given a singly linked list and an integer K, the task is to rotate the linked list clockwise to the right by K places.Examples: Input: 1 -> 2 -> 3 -> 4 -> 5 -> NULL, K = 2 Output: 4 -> 5 -> 1 -> 2 -> 3 -> NULLInput: 7 -> 9 -> 11 -> 13 -> 3 -> 5 -> NULL
15+ min read
Minimum time to reach given points on X-axis Given an array pos[] that represents N points on the X axis, a variable cur that represents the current position, and an array time. Where time[i] denotes the time taken to cover 1 unit of distance toward ith point, the task is to reach at any of the given points in minimum time and print the minimu
5 min read
Minimum cost to reach a point N from 0 with two different operations allowed Given integers N, P and Q where N denotes the destination position. The task is to move from position 0 to position N with minimum cost possible and print the calculated cost. All valid movements are: From position X you can go to position X + 1 with a cost of POr, you can go to the position 2 * X w
6 min read