Final Destination Hackerearth Problem coderinme

Final Destination

Bob and Khatu are stuck in matrix. The command center sent them a string which decodes to the their final destination. Since Bob and Khatu are not good at problem solving help them to figure out their final destination. They are initially at (0, 0). String contains L, R, U, D denoting left, right, up and down. In each command they will traverse 1 unit distance in the respective direction. For example if they are at (2, 0) and the command is L they will go to (1, 0).


Input contains a single string.


Print the final destination location of Bob and Khatu.


1 ≤ |S| ≤ 105
if the input string is “LLRDDR”
Initail Postion : 0, 0
1.) ‘L’ -> cover one unit of distance in left direction. New position (-1,0)
2.) ‘L’ -> new position (-2,0)
3.) ‘R’ -> new position (-1,0)
4.) ‘D’ -> new position (-1,-1)
5.) ‘D’ -> new position (-1,-2)
6.) ‘R’ -> new position (0,-2)

#include <iostream>
#include <string>
using namespace std;
int main()
string s;
int x=0,y=0;
for(int i=0;i<s.length();i++)

else if(s[i]=='R')

else if(s[i]=='U')
else if(s[i]=='D')

cout<<x<<" "<<y;

return 0;

 Final Destination

Competitive coding
Hackerearth problem

A web developer(Front end and Back end), and DBA at Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me

Leave a reply:

Your email address will not be published.