Displacement from origin after N moves of given distances in specified directions
Last Updated :
14 Jun, 2021
Given an array A[] consisting of characters ‘U’, ‘D’, ‘L’, and ‘R’ representing directions up, down, left, and right, and another array B[] consisting of N positive integers, the task is to find the displacement of a robot, starting its journey from (0, 0) facing toward North, after N moves, where in the ith move, the robot moves a distance B[i] in the direction A[i].
Examples:
Input: A[] = {U, R, R, R, R}, B[] = {1, 1, 1, 1, 0}
Output: 0 North
Explanation:
Initially, the robot is at (0, 0) facing North.
After 1st move, the position is (0, 1) facing North.
After 2nd move, the position is (1, 1) facing East.
After 3rd move, the position is (1, 0) facing South.
After 4th move, the position is (0, 0) facing West.
After 5th move, the position is (0, 0) facing North.
Therefore, the displacement is 0 and the final direction is North.
Input: A[] = {U, L, R, D, R}, B[] = {5, 5, 5, 5, 5}
Output: 11 West
Approach: The given problem can be solved by traversing the given array and keep the track of the direction and the distance traveled in the direction North(N), South(S), East(E), and West(W). Follow the steps below to solve the problem:
- Initialize variables, say N, S, E, and W that stores the distances traversed in the directions North, South, East, and West respectively after given set of movements.
- Initialize a variable, say P as North, that stores the final direction after performing the given set of movements.
- Traverse the given arrays A[] and B[] simultaneously and update the value of distance traverse in all the directions with the current direction.
- Now, the vertical displacement is given by (N – S) and the horizontal displacement is given by (E – S).
- After completing the above steps, print the final displacement as and the final direction stored in P.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void finalPosition( char a[], int b[],
int M)
{
int n = 0, s = 0, e = 0, w = 0;
char p = 'N' ;
for ( int i = 0; i < M; i++) {
if (p == 'N' ) {
if (a[i] == 'U' ) {
p = 'N' ;
n = n + b[i];
}
else if (a[i] == 'D' ) {
p = 'S' ;
s = s + b[i];
}
else if (a[i] == 'R' ) {
p = 'E' ;
e = e + b[i];
}
else if (a[i] == 'L' ) {
p = 'W' ;
w = w + b[i];
}
}
else if (p == 'S' ) {
if (a[i] == 'U' ) {
p = 'S' ;
s = s + b[i];
}
else if (a[i] == 'D' ) {
p = 'N' ;
n = n + b[i];
}
else if (a[i] == 'R' ) {
p = 'W' ;
w = w + b[i];
}
else if (a[i] == 'L' ) {
p = 'E' ;
e = e + b[i];
}
}
else if (p == 'E' ) {
if (a[i] == 'U' ) {
p = 'E' ;
e = e + b[i];
}
else if (a[i] == 'D' ) {
p = 'W' ;
w = w + b[i];
}
else if (a[i] == 'R' ) {
p = 'S' ;
s = s + b[i];
}
else if (a[i] == 'L' ) {
p = 'N' ;
n = n + b[i];
}
}
else if (p == 'W' ) {
if (a[i] == 'U' ) {
p = 'W' ;
w = w + b[i];
}
else if (a[i] == 'D' ) {
p = 'E' ;
e = e + b[i];
}
else if (a[i] == 'R' ) {
p = 'N' ;
n = n + b[i];
}
else if (a[i] == 'L' ) {
p = 'S' ;
s = s + b[i];
}
}
}
int ver_disp = n - s;
int hor_disp = e - w;
int displacement = floor (
sqrt ((ver_disp * ver_disp)
+ (hor_disp * hor_disp)));
cout << displacement << " " << p;
}
int main()
{
char A[] = { 'U' , 'R' , 'R' , 'R' , 'R' };
int B[] = { 1, 1, 1, 1, 0 };
int N = sizeof (A) / sizeof (B[0]);
finalPosition(A, B, N);
return 0;
}
|
Java
class GFG{
static void finalPosition( char [] a, int [] b, int M)
{
int n = 0 , s = 0 , e = 0 , w = 0 ;
char p = 'N' ;
for ( int i = 0 ; i < M; i++)
{
if (p == 'N' )
{
if (a[i] == 'U' )
{
p = 'N' ;
n = n + b[i];
}
else if (a[i] == 'D' )
{
p = 'S' ;
s = s + b[i];
}
else if (a[i] == 'R' )
{
p = 'E' ;
e = e + b[i];
}
else if (a[i] == 'L' )
{
p = 'W' ;
w = w + b[i];
}
}
else if (p == 'S' )
{
if (a[i] == 'U' )
{
p = 'S' ;
s = s + b[i];
}
else if (a[i] == 'D' )
{
p = 'N' ;
n = n + b[i];
}
else if (a[i] == 'R' )
{
p = 'W' ;
w = w + b[i];
}
else if (a[i] == 'L' )
{
p = 'E' ;
e = e + b[i];
}
}
else if (p == 'E' )
{
if (a[i] == 'U' )
{
p = 'E' ;
e = e + b[i];
}
else if (a[i] == 'D' )
{
p = 'W' ;
w = w + b[i];
}
else if (a[i] == 'R' )
{
p = 'S' ;
s = s + b[i];
}
else if (a[i] == 'L' )
{
p = 'N' ;
n = n + b[i];
}
}
else if (p == 'W' )
{
if (a[i] == 'U' )
{
p = 'W' ;
w = w + b[i];
}
else if (a[i] == 'D' )
{
p = 'E' ;
e = e + b[i];
}
else if (a[i] == 'R' )
{
p = 'N' ;
n = n + b[i];
}
else if (a[i] == 'L' )
{
p = 'S' ;
s = s + b[i];
}
}
}
int ver_disp = n - s;
int hor_disp = e - w;
int displacement = ( int )Math.ceil(Math.sqrt(
(ver_disp * ver_disp) + (hor_disp * hor_disp)));
System.out.print(displacement + " " + p);
}
public static void main(String args[])
{
char [] A = { 'U' , 'R' , 'R' , 'R' , 'R' };
int [] B = { 1 , 1 , 1 , 1 , 0 };
int N = 1 ;
finalPosition(A, B, N);
}
}
|
Python3
from math import sqrt, floor
def finalPosition(a, b, M):
n = 0
s = 0
e = 0
w = 0
p = 'N'
for i in range (M):
if (p = = 'N' ):
if (a[i] = = 'U' ):
p = 'N'
n = n + b[i]
elif (a[i] = = 'D' ):
p = 'S'
s = s + b[i]
elif (a[i] = = 'R' ):
p = 'E'
e = e + b[i]
elif (a[i] = = 'L' ):
p = 'W'
w = w + b[i]
elif (p = = 'S' ):
if (a[i] = = 'U' ):
p = 'S'
s = s + b[i]
elif (a[i] = = 'D' ):
p = 'N'
n = n + b[i]
elif (a[i] = = 'R' ):
p = 'W'
w = w + b[i]
elif (a[i] = = 'L' ):
p = 'E'
e = e + b[i]
elif (p = = 'E' ):
if (a[i] = = 'U' ):
p = 'E'
e = e + b[i]
elif (a[i] = = 'D' ):
p = 'W'
w = w + b[i]
elif (a[i] = = 'R' ):
p = 'S'
s = s + b[i]
elif (a[i] = = 'L' ):
p = 'N'
n = n + b[i]
elif (p = = 'W' ):
if (a[i] = = 'U' ):
p = 'W'
w = w + b[i]
elif (a[i] = = 'D' ):
p = 'E'
e = e + b[i]
elif (a[i] = = 'R' ):
p = 'N'
n = n + b[i]
elif (a[i] = = 'L' ):
p = 'S'
s = s + b[i]
ver_disp = n - s
hor_disp = e - w
displacement = floor(sqrt((ver_disp * ver_disp) +
(hor_disp * hor_disp)) + 1 )
print (displacement,p)
if __name__ = = '__main__' :
A = [ 'U' , 'R' , 'R' , 'R' , 'R' ]
B = [ 1 , 1 , 1 , 1 , 0 ]
N = len (A)
finalPosition(A, B, N)
|
C#
using System;
class GFG{
static void finalPosition( char [] a, int [] b, int M)
{
int n = 0, s = 0, e = 0, w = 0;
char p = 'N' ;
for ( int i = 0; i < M; i++)
{
if (p == 'N' )
{
if (a[i] == 'U' )
{
p = 'N' ;
n = n + b[i];
}
else if (a[i] == 'D' )
{
p = 'S' ;
s = s + b[i];
}
else if (a[i] == 'R' )
{
p = 'E' ;
e = e + b[i];
}
else if (a[i] == 'L' )
{
p = 'W' ;
w = w + b[i];
}
}
else if (p == 'S' )
{
if (a[i] == 'U' )
{
p = 'S' ;
s = s + b[i];
}
else if (a[i] == 'D' )
{
p = 'N' ;
n = n + b[i];
}
else if (a[i] == 'R' )
{
p = 'W' ;
w = w + b[i];
}
else if (a[i] == 'L' )
{
p = 'E' ;
e = e + b[i];
}
}
else if (p == 'E' )
{
if (a[i] == 'U' )
{
p = 'E' ;
e = e + b[i];
}
else if (a[i] == 'D' )
{
p = 'W' ;
w = w + b[i];
}
else if (a[i] == 'R' )
{
p = 'S' ;
s = s + b[i];
}
else if (a[i] == 'L' )
{
p = 'N' ;
n = n + b[i];
}
}
else if (p == 'W' )
{
if (a[i] == 'U' )
{
p = 'W' ;
w = w + b[i];
}
else if (a[i] == 'D' )
{
p = 'E' ;
e = e + b[i];
}
else if (a[i] == 'R' )
{
p = 'N' ;
n = n + b[i];
}
else if (a[i] == 'L' )
{
p = 'S' ;
s = s + b[i];
}
}
}
int ver_disp = n - s;
int hor_disp = e - w;
int displacement = ( int )Math.Ceiling(Math.Sqrt(
(ver_disp * ver_disp) + (hor_disp * hor_disp)));
Console.WriteLine(displacement + " " + p);
}
public static void Main()
{
char [] A = { 'U' , 'R' , 'R' , 'R' , 'R' };
int [] B = { 1, 1, 1, 1, 0 };
int N = 1;
finalPosition(A, B, N);
}
}
|
Javascript
<script>
function finalPosition(a, b, M)
{
let n = 0, s = 0, e = 0, w = 0;
let p = 'N' ;
for (let i = 0; i < M; i++)
{
if (p == 'N' )
{
if (a[i] == 'U' )
{
p = 'N' ;
n = n + b[i];
}
else if (a[i] == 'D' )
{
p = 'S' ;
s = s + b[i];
}
else if (a[i] == 'R' )
{
p = 'E' ;
e = e + b[i];
}
else if (a[i] == 'L' )
{
p = 'W' ;
w = w + b[i];
}
}
else if (p == 'S' )
{
if (a[i] == 'U' )
{
p = 'S' ;
s = s + b[i];
}
else if (a[i] == 'D' )
{
p = 'N' ;
n = n + b[i];
}
else if (a[i] == 'R' )
{
p = 'W' ;
w = w + b[i];
}
else if (a[i] == 'L' )
{
p = 'E' ;
e = e + b[i];
}
}
else if (p == 'E' )
{
if (a[i] == 'U' )
{
p = 'E' ;
e = e + b[i];
}
else if (a[i] == 'D' )
{
p = 'W' ;
w = w + b[i];
}
else if (a[i] == 'R' )
{
p = 'S' ;
s = s + b[i];
}
else if (a[i] == 'L' )
{
p = 'N' ;
n = n + b[i];
}
}
else if (p == 'W' )
{
if (a[i] == 'U' )
{
p = 'W' ;
w = w + b[i];
}
else if (a[i] == 'D' )
{
p = 'E' ;
e = e + b[i];
}
else if (a[i] == 'R' )
{
p = 'N' ;
n = n + b[i];
}
else if (a[i] == 'L' )
{
p = 'S' ;
s = s + b[i];
}
}
}
let ver_disp = n - s;
let hor_disp = e - w;
let displacement = Math.ceil(Math.sqrt(
(ver_disp * ver_disp) + (hor_disp * hor_disp)));
document.write(displacement + " " + p);
}
let A = [ 'U' , 'R' , 'R' , 'R' , 'R' ];
let B = [ 1, 1, 1, 1, 0 ];
let N = 1;
finalPosition(A, B, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...