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
Be the first one to answer
Add answer anonymously...
CGI Group Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter