Final cell position in the matrix
Last Updated :
01 Sep, 2022
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)
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void finalPos( char command[], int n,
int x, int y)
{
int cup, cdown, cleft, cright;
int final_x, final_y;
cup = cdown = cleft = cright = 0;
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++;
}
final_x = x + (cright - cleft);
final_y = y + (cdown - cup);
cout << "Final Position: "
<< "("
<< final_x << ", " << final_y << ")" ;
}
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
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
static void finalPos(String command, int n,
int x, int y)
{
int cup, cdown, cleft, cright;
int final_x, final_y;
cup = cdown = cleft = cright = 0 ;
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++;
}
final_x = x + (cright - cleft);
final_y = y + (cdown - cup);
System.out.println( "Final Position: " +
"(" + final_x + ", " +
final_y + ")" );
}
public static void main(String []args)
{
String command = "DDLRULL" ;
int n = command.length();
int x = 3 , y = 4 ;
finalPos(command, n, x, y);
}
}
|
Python3
def finalPos(command, n, x, y):
cup = 0
cdown = 0
cleft = 0
cright = 0 ;
for i in range (n):
if (command[i] = = 'U' ):
cup + = 1
elif (command[i] = = 'D' ):
cdown + = 1
elif (command[i] = = 'L' ):
cleft + = 1
elif (command[i] = = 'R' ):
cright + = 1
final_x = x + (cright - cleft);
final_y = y + (cdown - cup);
print ( "Final Position:" , "(" , final_x, "," , final_y, ")" );
command = "DDLRULL" ;
n = len (command);
x = 3
y = 4 ;
finalPos(command, n, x, y);
|
C#
class GFG
{
static void finalPos( string command, int n,
int x, int y)
{
int cup, cdown, cleft, cright;
int final_x, final_y;
cup = cdown = cleft = cright = 0;
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++;
}
final_x = x + (cright - cleft);
final_y = y + (cdown - cup);
System.Console.WriteLine( "Final Position: " +
"(" + final_x + ", " +
final_y + ")" );
}
public static void Main()
{
string command = "DDLRULL" ;
int n = command.Length;
int x = 3, y = 4;
finalPos(command, n, x, y);
}
}
|
PHP
<?php
function finalPos( $command , $n , $x , $y )
{
$cup ; $cdown ; $cleft ; $cright ;
$final_x ; $final_y ;
$cup = $cdown = $cleft = $cright = 0;
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 ++;
}
$final_x = $x + ( $cright - $cleft );
$final_y = $y + ( $cdown - $cup );
echo "Final Position: " . "(" .
$final_x . ", " .
$final_y . ")" ;
}
$command = "DDLRULL" ;
$n = strlen ( $command );
$x = 3; $y = 4;
finalPos( $command , $n , $x , $y );
|
Javascript
<script>
function finalPos(command, n, x, y)
{
let cup, cdown, cleft, cright;
let final_x, final_y;
cup = cdown = cleft = cright = 0;
for (let 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++;
}
final_x = x + (cright - cleft);
final_y = y + (cdown - cup);
document.write( "Final Position: " +
"(" + final_x + ", " +
final_y + ")" );
}
let command = "DDLRULL" ;
let n = command.length;
let x = 3, y = 4;
finalPos(command, n, x, y);
</script>
|
OutputFinal Position: (1, 5)
Complexity Analysis:
- Time Complexity: O(n), where is the number of commands.
- Space complexity: O(1) since using constant variables
Share your thoughts in the comments
Please Login to comment...