# Counting Frog Paths | Hackerearth Solution

Problem:-

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 $\left(x,y\right)$, the frog can spend $1$ second to move to:

• $\left(x+1,y\right)$
• $\left(x,y+1\right)$
• $\left(x,y\right)$

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 $\left(X,Y\right)$$\left(X+s,Y\right)$$\left(X,Y+s\right)$$\left(X+s,Y+s\right)$. Calculate how many points with integer coordinates on or inside this square could be the frog's position after exactly $T$ seconds

Input Format:

The first and only line of input contains four space-separated integers: $X$$Y$$s$, and $T$.

Output Format:

Print the number of points with integer coordinates that could be the frog's position after $T$ seconds.

Constraints:

$0\le X,Y\le 100$

$1\le s\le 100$

$0\le T\le 400$

Note that the Expected Output Feature for Custom Invocation is not supported for this contest.

Sample Input
2 2 3 6
Sample Output
6
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The points shown are the points on or in the specified square, and those that are red are the points that the frog could be at after $6$ seconds.

Code:-

Solution 1 ( C language):-

// Sample code to perform I/O:
#include <stdio.h>
int main(){
int x,y,s,t,count=0;
scanf("%d", &x);
scanf("%d", &y);
scanf("%d", &s);
scanf("%d", &t);             // Reading input from STDIN

for(int i=x;i<=x+s;i++){
for(int j=y;j<=y+s;j++){
if(i+j<=t)
count++;
}
}
printf("%d",count);
}

Solution 2 ( C++ language):-

#include<bits/stdc++.h>
using namespace std;
int main()
{
int x,y,s,t,count=0,points;
cin>>x>>y>>s>>t;
for(int i=x;i<=x+s;i++)
{
for(int j=y;j<=y+s;j++)
{
if(i+j<=t)
count++;
}
}cout<<count<<endl;
}

Solution 3 (java language):-

class TestClass {
public static void main(String args[] ) throws Exception {
int x = Integer.parseInt(arr[0]);
int y = Integer.parseInt(arr[1]);
int s = Integer.parseInt(arr[2]);
int t = Integer.parseInt(arr[3]);
int count = 0;
for(int i = y; i <= y+s ; i++){
for(int j = x; j <= x+s ; j++){
if(i+j<=t){
count ++;
}
}
}
System.out.println(count);
}
}

keywords:-

,counting frog paths hackerearth solution,frog path code in python,,frog path in python,counting closed paths hackerrank solution,count paths hackerearth solution,counting closed paths in c hackerrank,counting closed paths hackerrank solution in c,

Recommended Post:-

• codechef problems:-

Wipro :-

Infytq :-

Key Points;-

Hackerrank:-

C-tutorial:-

See more:-