Frog Jump Problem Statement

A frog is positioned on the first step of a staircase consisting of N steps. The goal is for the frog to reach the final step, i.e., the Nth step. The height of each step is provided in the array HEIGHT. If the frog jumps from the ith step to the jth step, the energy expended during the jump is represented by the absolute difference |HEIGHT[i-1] - HEIGHT[j-1]|. From the ith step, the frog can jump to either the (i+1)th step or the (i+2)th step. The task is to determine the minimum total energy expenditure required for the frog to travel from the first step to the Nth step.

Input:

The first line of input contains an integer 'T,' indicating the number of test cases.
Each test case starts with an integer 'N,' the number of stairs in the staircase.
The following line comprises the 'HEIGHT' array, specifying the heights of each stair step.

Output:

For each test case, return a single integer denoting the minimal energy required to reach the last stair.

Example:

Input:
HEIGHT = [10,20,30,10]
Output:
20
Explanation:

The frog can jump from the 1st stair to the 2nd stair, incurring an energy cost of |20-10| = 10. Subsequently, it jumps from the 2nd stair to the last stair, at an energy cost of |10-20| = 10. The total energy expended is thus 20.

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 100000
  • 1 ≤ HEIGHT[i] ≤ 1000
  • Time limit: 1 sec
AnswerBot
4mo

Calculate the minimal energy required for a frog to travel from the first step to the last step of a staircase.

  • Iterate through the staircase steps and calculate the energy cost for each jump.

  • Keep trac...read more

Help your peers!
Select
Add answer anonymously...

CGI Group Associate Software Engineer interview questions & answers

An Associate Software Engineer was asked 2mo agoQ. Tell me about your college experience.
An Associate Software Engineer was asked 3mo agoQ. What are the core principles of Object-Oriented Programming (OOP) in Java?
An Associate Software Engineer was asked 6mo agoQ. Write Java code to demonstrate string manipulation.

Popular interview questions of Associate Software Engineer

An Associate Software Engineer was asked 2mo agoQ1. Tell me about your college experience.
An Associate Software Engineer was asked 3mo agoQ2. What are the core principles of Object-Oriented Programming (OOP) in Java?
An Associate Software Engineer was asked 6mo agoQ3. Write Java code to demonstrate string manipulation.

Top HR questions asked in CGI Group Associate Software Engineer

An Associate Software Engineer was asked 1mo agoQ1. What are your strengths and weaknesses?
An Associate Software Engineer was asked 5mo agoQ2. What actions would you take if your manager does not approve your product idea, ...read more
An Associate Software Engineer was asked 5mo agoQ3. Can you provide examples of real-life scenarios where you handled conflicts with...read more
CGI Group Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits