# Find the final co-ordinates reached by following a sequence of directions

Given a starting point with x and y co-ordinates SX and SY respectively, and a sequence ‘D’ denoting the directions to be followed, the task is to find the co-ordinate of the destination. The string D consists of characters S,N,W and E where [ S = South (move down one unit), N = North (move up one unit), W = West (move left one unit), E = East (move right one unit) ]

Examples .

Input: SX = 2, SY = 3, D = “SEWW”
Output: (1,2)
Explanation: From the given point, it will be directed
to S(2, 2)->E(3, 2)->W(2, 2)->W(1, 2). Therefore the final position is (1,2)

Input: SX = 2, SY = 2, D = “NSSE”
Output: (3,1)

## Recommended: Please solve it on {IDE} first, before moving on to the solution.

Approach: Traverse through the string D and increment or decrement the co-ordinates SX and SY accordingly and print their final values.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of ` `// the above approach ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to print the final position ` `// of the point after traversing through  ` `// the given directions ` `void` `finalCoordinates(``int` `SX, ``int` `SY, string D){ ` `     `  `    ``// Traversing through the given directions  ` `    ``for` `(``int` `i = 0; i < D.length(); i++){ ` `         `  `        ``// If its north or south the point  ` `        ``// will move left or right  ` `        ``if` `(D[i] == ``'N'``) ` `            ``SY += 1; ` `        ``else` `if``(D[i] == ``'S'``) ` `            ``SY -= 1; ` `             `  `        ``// If its west or east the point  ` `        ``// will move upwards or downwards ` `        ``else` `if``(D[i] == ``'E'``) ` `            ``SX += 1; ` `        ``else` `            ``SX -= 1; ` `    ``} ` `         `  `    ``// Returning the final position ` `    ``string ans = ``'('` `+ to_string(SX) + ``','` `+ ` `                     ``to_string(SY) + ``')'``; ` `    ``cout<

## Java

 `// Java implementation of the above approach ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function to print the final position ` `// of the point after traversing through  ` `// the given directions ` `static` `void` `finalCoordinates(``int` `SX, ``int` `SY, ` `                             ``char` `[]D) ` `{ ` `     `  `    ``// Traversing through the given directions  ` `    ``for``(``int` `i = ``0``; i < D.length; i++) ` `    ``{ ` `        `  `       ``// If its north or south the point  ` `       ``// will move left or right  ` `       ``if` `(D[i] == ``'N'``) ` `           ``SY += ``1``; ` `       ``else` `if``(D[i] == ``'S'``) ` `           ``SY -= ``1``; ` `        `  `       ``// If its west or east the point  ` `       ``// will move upwards or downwards ` `       ``else` `if``(D[i] == ``'E'``) ` `           ``SX += ``1``; ` `       ``else` `           ``SX -= ``1``; ` `    ``} ` `         `  `    ``// Returning the final position ` `    ``String ans = ``'('` `+ String.valueOf(SX) + ``','` `+ ` `                       ``String.valueOf(SY) + ``')'``; ` `    ``System.out.print(ans); ` `} ` `     `  `// Driver Code  ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `SX = ``2``, SY = ``2``; ` `    ``String D = ``"NSSE"``; ` `     `  `    ``finalCoordinates(SX, SY, D.toCharArray()); ` `}  ` `} ` ` `  `// This code is contributed by gauravrajput1 `

## Python3

 `# Python3 implementation of ` `# the above approach ` ` `  `# Function to print the final position ` `# of the point after traversing through  ` `# the given directions ` `def` `finalCoordinates(SX, SY, D): ` `     `  `    ``# Traversing through the given directions  ` `    ``for` `i ``in` `range` `(``len``(D)): ` `         `  `        ``# If its north or south the point  ` `        ``# will move left or right  ` `        ``if` `(D[i] ``=``=` `'N'``): ` `            ``SY ``+``=` `1` `        ``elif` `(D[i] ``=``=` `'S'``): ` `            ``SY ``-``=` `1` `             `  `        ``# If its west or east the point  ` `        ``# will move upwards or downwards ` `        ``elif` `(D[i] ``=``=` `'E'``): ` `            ``SX ``+``=` `1` `        ``else` `: ` `            ``SX ``-``=` `1` ` `  `    ``# Returning the final position ` `    ``ans ``=` `'('` `+` `str``(SX) ``+` `','` `+`  `str``(SY) ``+` `')'` `    ``print` `(ans) ` `     `  `# Driver Code  ` `if` `__name__ ``=``=` `'__main__'``:  ` `    ``SX, SY ``=` `2``,``2`   `    ``D ``=` `"NSSE"` `     `  `    ``finalCoordinates(SX, SY, D) ` `     `  ` `  `# This code is contributed by parna_28 `

## C#

 `// C# implementation of the above approach ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to print the readonly position ` `// of the point after traversing through  ` `// the given directions ` `static` `void` `finalCoordinates(``int` `SX, ``int` `SY, ` `                             ``char` `[]D) ` `{ ` `     `  `    ``// Traversing through the given directions  ` `    ``for``(``int` `i = 0; i < D.Length; i++) ` `    ``{ ` `        `  `       ``// If its north or south the point  ` `       ``// will move left or right  ` `       ``if` `(D[i] == ``'N'``) ` `       ``{ ` `           ``SY += 1; ` `       ``} ` `       ``else` `if``(D[i] == ``'S'``) ` `       ``{ ` `           ``SY -= 1; ` `       ``} ` `        `  `       ``// If its west or east the point  ` `       ``// will move upwards or downwards ` `       ``else` `if``(D[i] == ``'E'``) ` `       ``{ ` `           ``SX += 1; ` `       ``} ` `       ``else` `       ``{ ` `           ``SX -= 1; ` `       ``} ` `    ``} ` `     `  `    ``// Returning the readonly position ` `    ``String ans = ``'('` `+ String.Join(``""``, SX) + ``','` `+ ` `                       ``String.Join(``""``, SY) + ``')'``; ` `    ``Console.Write(ans); ` `} ` `     `  `// Driver Code  ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `SX = 2, SY = 2; ` `    ``String D = ``"NSSE"``; ` `     `  `    ``finalCoordinates(SX, SY, D.ToCharArray()); ` `}  ` `} ` ` `  `// This code is contributed by gauravrajput1 `

Output:

```(3,1)
``` My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : ipg2016107, GauravRajput1

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.