Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Final Destination problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth Final Destination problem solution, Bob and Khatu are stuck in a matrix. The command center sent them a string that decodes to 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). The 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 they will go to (1, 0).
 
 
HackerEarth Final Destination problem solution

 

 

HackerEarth Final Destination problem solution.

#include <bits/stdc++.h>
using namespace std;
int main()
{
string S;
int X=0, Y=0;
cin>>S;
for(int i=0;i<S.size(); i++)
{
if(S[i]=='L')
X--;
else if(S[i]=='R')
X++;
else if(S[i]=='U')
Y++;
else if(S[i]=='D')
Y--;
else
assert(0);
}
cout<<X<<" "<<Y<<endl;
return 0;
}
 
 

Second solution

#include<bits/stdc++.h>

using namespace std;

#define vi vector < int >
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define foreach(it,v) for( __typeof((v).begin())it = (v).begin() ; it != (v).end() ; it++ )
#define ll long long
#define llu unsigned long long
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define dbg(x) { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y) { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define all(x) x.begin(),x.end()
#define mset(x,v) memset(x, v, sizeof(x))
#define sz(x) (int)x.size()

int main()
{
string s;
cin >> s;
int x = 0 , y = 0;
int n = sz(s) , i;
assert(1 <= n && n <= 100000);
for(i=0;i<n;i++)
{
if(s[i] == 'L')
{
x--;
}
else if(s[i] == 'R')
{
x++;
}
else if(s[i] == 'U')
{
y++;
}
else if(s[i] == 'D')
{
y--;
}
else
{
assert(0);
}
}
cout << x << " " << y << endl;
return 0;
}
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes