Header Ads Widget

Counting Frog Paths | Hackerearth 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:

  • (x+1,y)
  • (x,y+1)
  • (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

Input Format:

The first and only line of input contains four space-separated integers: XYs, and T.

Output Format:

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





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

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

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.


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++){

Solution 2 ( C++ language):-

using namespace std;
int main()
    int x,y,s,t,count=0,points;
    for(int i=x;i<=x+s;i++)
        for(int j=y;j<=y+s;j++)

Solution 3 (java language):-

import java.io.BufferedReader;
import java.io.InputStreamReader;
class TestClass {
public static void main(String args[] ) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String arr[] = br.readLine().split(" ");
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++){
count ++;


  ,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:-

Post a Comment