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 The Perfect Road problem solution

YASH PAL, 31 July 202413 February 2026
In this HackerEarth The Perfect Road problem solution we have given that, there are N paths numbered from 1 to N, where each path connects the starting point S to the destination point X. The maximum speed limit is speed[i] for the ith path which can be used to reach the destination.
 
Find the path to reach the destination as early as possible. If there are multiple paths from which you can choose, print Many Roads. Also, it is guaranteed that X will always be greater than S.
 
 
HackerEarth The Perfect Road problem solution

 

 

HackerEarth The Perfect Road problem solution.

#include<iostream>
#include<algorithm>
#include<functional>
#include<utility>
#include<cstring>
#include<climits>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
#define MAX LONG_LONG_MAX
#define MIN LONG_LONG_MIN
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define INF 1e17
using namespace std;
const int mod=1e+7;
int FindPerfectRoad(int Startpoint,int X,vector<int> s)
{
double res=999999.000;
vector<double> ans;
int answer=0,counter=0,Diff=(X-Startpoint);
int n=s.size();
for(int i=0;i<n;i++)
{
double TimeReq=(Diff/(double)s[i]);
ans.push_back(TimeReq);
if(TimeReq<res)
{
res=TimeReq;
answer=i+1;
}
}
for(int i=0;i<n;i++)
if(ans[i]==res)
counter++;
if(counter==1)
return answer;
else
return -1;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,StartPoint,X;
cin>>StartPoint>>X>>n;
vector<int> s;
for(int i=0;i<n;i++)
{
int temp;
cin>>temp;
s.push_back(temp);
}
int ans=FindPerfectRoad(StartPoint,X,s);
if(ans==-1)
cout<<"Many Roads"<<endl;
else
cout<<ans<<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