Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerEarth Meeting the origin problem solution

YASH PAL, 31 July 2024
In this HackerEarth Meeting the origin problem solution You are given a string path S which consists of only four characters ‘L’, ‘R’, ‘U’, and ‘D’. You are initially at origin (0,0). You have to follow the directions as provided in path S in order. For ‘L’ you have to move 1 unit left, for ‘R’ move 1 unit right, for ‘U’ move 1 unit up and for ‘D’ move 1 unit down.
You can do these operations any number of times:
Delete any single move and remove it from the string path .
Convert any single move ‘L’ to ‘R’ or vice-versa.
Convert any single move ‘U’ to ‘D’ or vice-versa.
Print the minimum operations required, after which going through the new path, you will meet the origin again.
HackerEarth Meeting the origin problem solution

HackerEarth Meeting the origin problem solution.

#include<bits/stdc++.h>
using namespace std;

#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define vi vector<int>
#define all(x) x.begin(),x.end()
#define fix fixed<<setprecision(10)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define FastIO ios_base::sync_with_stdio(0),cin.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=1e9+7;

void solve(){
string s;
cin>>s;
int l=count(all(s),'L'),r=count(all(s),'R'),u=count(all(s),'U'),d=count(all(s),'D');
int ans=(l+r)%2+(u+d)%2+abs(l-r)/2+abs(u-d)/2;
cout<<ans<<'n';
}

signed main(){
FastIO;
int t;
cin>>t;
while(t--) solve();
return 0;
}

Second solution

t = int(input())

while t > 0:
t -= 1
s = input()
print((abs(s.count('L') - s.count('R')) + 1) // 2 + (abs(s.count('U') - s.count('D')) + 1) // 2)
coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes