Frog in the well

There is a  frog in a well having 30-meter depth. One day frog plan to escape the well in a day frog can jump only 3 meters of height and tired up and took rest the whole night, in the night he sleeps down by two-meter of height. continuously frog did his duty daily.

So the question is how many days the frog took to get out of the well.

Example:

Input:  depth=30 
Output: 28 days

Approach

Java


public class FrogWell {


    static int  depthOfWell=30;//in meter

    static int disClimbInDay=3;//in meter

    static int slipInNight=2;//in meter

    static int noOfDays0;
    
    public static void main(String[] args) {
        
        System.out.println(calculate(0));

    }

    private static int calculate(int distCovered) {

        noOfDays++;

        //during day
        distCovered=distCovered+disClimbInDay;

        while(distCovered<depthOfWell) {

            //during night
            distCovered= distCovered-slipInNight;

            calculate(distCovered);

            return noOfDays;
        }
        return noOfDays;
    
        
    }
}



No comments:

Post a Comment