Skip to content
Programming101
Programming101

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
Programming101

Learn everything about programming

HackerEarth Counting Frog Paths problem solution

YASH PAL, 31 July 2024
In this HackerEarth Counting Frog Paths problem solution There is a frog initially placed at the origin of the coordinate plane. In exactly 1 second, the frog can either move up 1 unit, move right 1 unit, or stay still. In other words, from position (x,y), the frog can spend 1 second to move to:
  1. (x+1, y)
  2. (x, y+1)
  3. (x, y)
After T seconds, a villager who sees the frog reports that the frog lies on or inside a square of side-length s with coordinates (X, Y), (X+s, Y), (X, Y+s), (X+s, Y+s. Calculate how many points with integer coordinates on or inside this square could be the frog’s position after exactly T seconds.
HackerEarth Counting Frog Paths problem solution

HackerEarth Counting Frog Paths problem solution.

#include <bits/stdc++.h>

using namespace std;


string twodig(int i){
string res = "";
while(i>0){
res = char('0' + i%10) + res;
i/=10;
}
while(res.length() < 2) res = "0"+res;
return res;
}
int solve(int X, int Y, int s, int T){
int counter = 0;
for(int positionX = X; positionX <= X+s; positionX++){
for(int positionY = Y; positionY <= Y+s; positionY++){
if(positionY + positionX <= T){
counter++;
}
}
}
return counter;
}
void gen(){
int iters = 50;
for(int i = 6; i <= iters; i++){
ofstream fout("in"+twodig(i)+".txt");
int x = rand()%101, y = rand()%101, s = (1 + rand()%100), t = rand()%401;
fout << x << " " << y << " "<< s << " " << t << endl;
fout.close();
}
}
void solveall(){
int iters = 50;
for(int i = 1; i <= iters; i++){
ifstream fin("in"+twodig(i)+".txt");
int x,y,s,t;
fin >> x >> y >> s >> t;
int ans = solve(x,y,s,t);
ofstream fout("out"+twodig(i)+".txt");
fout << ans << endl;
fout.close();
fin.close();
}
}
void validateall(){
int iters = 50;
for(int i = 1; i <= iters; i++){
ifstream fin("in"+twodig(i)+".txt");
int x,y,s,t;
fin >> x >> y >> s >> t;
assert(0<=x);
assert(x<=100);
assert(0<=y);
assert(y<=100);
assert(1<=s);
assert(s<=100);
assert(0<=t);
assert(t<=400);
}
}
int main()
{
int x,y,s,t;
cin >> x >> y >> s >> t;
cout << solve(x,y,s,t) << endl;
}

Second solution

#include<bits/stdc++.h>

using namespace std;

typedef complex<double> base;
typedef long double ld;
typedef long long ll;

#define pb push_back
#define pii pair<int,int>
#define pll pair< ll , ll >
#define vi vector<int>

const int maxn=(int)(1e5+5);
const ll mod=(ll)(998244353);
int a[maxn];

int main()
{
ios_base::sync_with_stdio(0);

int x,y,s,t,res=0;

cin>>x>>y>>s>>t;

assert(min(x,y)>=0 && max(x,y)<=100);

assert(s>=1 && s<=100);

assert(t>=0 && t<=400);

for(int i=x;i<=x+s;i++)
{
for(int j=y;j<=y+s;j++)
{
int dis=i+j;

if(dis<=t)
{
res++;
}
}
}

cout<<res<<endl;

}
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
©2025 Programming101 | WordPress Theme by SuperbThemes