Given an array of list of commands U(Up), D(Down), L(Left) and R(Right) and initial cell position (x, y) in a matrix. Find the final cell position of the object in the matrix after following the given commands. It is assumed that the final required cell position exists in the matrix.
Examples:
Input : command[] = "DDLRULL"
x = 3, y = 4
Output : (1, 5)
Input : command[] = "LLRUUUDRRDDDULRLLUDUUR"
x = 6, y = 5
Output : (6, 3)
Source: Flipkart Interview (SDE-1 On Campus).
Approach: Following are the steps:
- Count cup, cdown, cleft and cright for U(Up), D(Down), L(Left) and R(Right) movements respectively.
- Calculate final_x = x + (cright – cleft) and final_y = y + (cdown – cup).
The final cell position is (final_x, final_y)
C++
// C++ implementation to find the final cell position // in the given matrix #include <bits/stdc++.h> using namespace std; // function to find the final cell position // in the given matrix void finalPos(char command[], int n, int x, int y) { // to count up, down, left and cright // movements int cup, cdown, cleft, cright; // to store the final coordinate position int final_x, final_y; cup = cdown = cleft = cright = 0; // traverse the command array for (int i = 0; i < n; i++) { if (command[i] == 'U') cup++; else if (command[i] == 'D') cdown++; else if (command[i] == 'L') cleft++; else if (command[i] == 'R') cright++; } // calculate final values final_x = x + (cright - cleft); final_y = y + (cdown - cup); cout << "Final Position: " << "(" << final_x << ", " << final_y << ")"; } // Driver program to test above int main() { char command[] = "DDLRULL"; int n = (sizeof(command) / sizeof(char)) - 1; int x = 3, y = 4; finalPos(command, n, x, y); return 0; } |
Java
// Java implementation to find // the final cell position // in the given matrix import java.util.*; import java.lang.*; import java.io.*; class GFG { // function to find the // final cell position // in the given matrix static void finalPos(String command, int n, int x, int y) { // to count up, down, left // and cright movements int cup, cdown, cleft, cright; // to store the final // coordinate position int final_x, final_y; cup = cdown = cleft = cright = 0; // traverse the command array for (int i = 0; i < n; i++) { if (command.charAt(i) == 'U') cup++; else if (command.charAt(i) == 'D') cdown++; else if (command.charAt(i)== 'L') cleft++; else if (command.charAt(i) == 'R') cright++; } // calculate final values final_x = x + (cright - cleft); final_y = y + (cdown - cup); System.out.println("Final Position: " + "(" + final_x + ", " + final_y + ")"); } // Driver Code public static void main(String []args) { String command = "DDLRULL"; int n = command.length(); int x = 3, y = 4; finalPos(command, n, x, y); } } // This code is contributed // by Subhadeep |
C#
// C# implementation to find // the final cell position // in the given matrix class GFG { // function to find the // final cell position // in the given matrix static void finalPos(string command, int n, int x, int y) { // to count up, down, left // and cright movements int cup, cdown, cleft, cright; // to store the final // coordinate position int final_x, final_y; cup = cdown = cleft = cright = 0; // traverse the command array for (int i = 0; i < n; i++) { if (command[i] == 'U') cup++; else if (command[i] == 'D') cdown++; else if (command[i]== 'L') cleft++; else if (command[i] == 'R') cright++; } // calculate final values final_x = x + (cright - cleft); final_y = y + (cdown - cup); System.Console.WriteLine("Final Position: " + "(" + final_x + ", " + final_y + ")"); } // Driver Code public static void Main() { string command = "DDLRULL"; int n = command.Length; int x = 3, y = 4; finalPos(command, n, x, y); } } // This code is contributed // by mits |
PHP
<?php // PHP implementation to find the final // cell position in the given matrix // function to find the final cell // position in the given matrix function finalPos($command, $n, $x, $y) { // to count up, down, left and // cright movements $cup; $cdown; $cleft; $cright; // to store the final coordinate // position $final_x; $final_y; $cup = $cdown = $cleft = $cright = 0; // traverse the command array for ($i = 0; $i < $n; $i++) { if ($command[$i] == 'U') $cup++; else if ($command[$i] == 'D') $cdown++; else if ($command[$i] == 'L') $cleft++; else if ($command[$i] == 'R') $cright++; } // calculate final values $final_x = $x + ($cright - $cleft); $final_y = $y + ($cdown - $cup); echo "Final Position: " . "(" . $final_x . ", " . $final_y . ")"; } // Driver Code $command = "DDLRULL"; $n = strlen($command); $x = 3; $y = 4; finalPos($command, $n, $x, $y); // This code is contributed // by Akanksha Rai |
Output:
Final Position: (1, 5)
Time Complexity: O(n), where
is the number of commands.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Final direction after visiting every cell of Matrix starting from (0, 0)
- Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Final Matrix after incrementing submatrices by K in range given by Q queries
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Distance of nearest cell having 1 in a binary matrix
- Find minimum moves to bring all elements in one cell of a matrix
- Number of ways of cutting a Matrix such that atleast one cell is filled in each part
- Sum of cost of all paths to reach a given cell in a Matrix
- Minimum Distance from a given Cell to all other Cells of a Matrix
- Find final value if we double after every successful search in array
- Final state of the string after modification
- Find the final X and Y when they are Altering under given condition
- Find the final radiations of each Radiated Stations
- Minimum possible final health of the last monster in a game
- Find the final sequence of the array after performing given operations
- Length of race track based on the final distance between participants
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.

