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
      • Data Structures Solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Big P and The Road Less Travelled problem solution

YASH PAL, 31 July 202417 February 2026
In this HackerEarth Big P and The Road Less Travelled problem solution Big P is willing to climb up a hill. The hill is divided into several checkpoints where the travelers can buy water,food etc for their trip.
 
At each checkpoint there are several roads that go up to other checkpoints (Not necessarily the next checkpoint).
 
Now Big P is standing at the base of the hill i.e at checkpoint 1 and wants to know the total number of ways he could climb to the top of the hill i.e the last checkpoint N.
 
 
HackerEarth Big P and The Road Less Travelled problem solution

 

 

HackerEarth Big P and The Road Less Travelled problem solution.

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;

class TestClass
{
static int n;
static int[] paths;
static int numPaths = 0;
static int[] startingPts;
static int[] endingPts;

public static void main (String[] args)
{

Scanner f = new Scanner(System.in);
n = f.nextInt ();
paths = new int [n + 1];
for (int i = 0 ; i < n + 1 ; i++)
paths [i] = -1;
startingPts = new int [1000000];
endingPts = new int [1000000];
numPaths = 0;
int x = f.nextInt ();
int y = f.nextInt ();
while (!(x == 0 && y == 0))
{
startingPts[numPaths] = x;
endingPts[numPaths] = y;
numPaths++;
x = f.nextInt ();
y = f.nextInt ();
}
System.out.println (findNumPaths (1));
}
static public int findNumPaths (int pt)
{
if (pt == n)
return 1;
else
{
int sum = 0;
for (int i = 0 ; i < numPaths ; i++)
{
if (startingPts [i] == pt)
{
int answer = paths [endingPts [i]];
if (answer != -1)
sum += answer;
else
{
int a = findNumPaths (endingPts [i]);
paths [endingPts [i]] = a;
sum += a;
}
}
}
return sum;
}
}
}
 

Second solution

#include <bits/stdc++.h>

using namespace std;

vector <int> v[100005];
int n;
long long dp[100005];
bool vis[100005];

long long f(int idx)
{
if ( idx == n ) return 1;
if ( vis[idx] ) return dp[idx];
vis[idx] = true;
long long ans = 0;
for ( int i = 0; i < v[idx].size(); i++ ) ans += f(v[idx][i]);
dp[idx] = ans;
return ans;
}

int main()
{
int x,y;
cin >> n;
for ( ;; ) {
cin >> x >> y;
if ( x == 0 && y == 0 ) break;
v[x].push_back(y);
}
long long ans = f(1);
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