DIY: Jump Game IV

Solve the interview question "Jump Game IV" in this lesson.

Problem statement

In this challenge, you are given an array, arr. Consider that a pointer is initially positioned at the first index of the array. Your task is to determine the minimum number of steps the pointer needs to take to reach the last index of the array. The pointer can take steps according to the following criteria:

  • The pointer can jump from index i to index i + 1, where i + 1 < arr.Length.
  • The pointer can jump from index i to index i - 1, where i - 1 >= 0.
  • The pointer can jump from index i to index j, where arr[i] == arr[j] and i != j.

Input

The function’s input will be an array called arr. The following is an example input:

arr = [23, 11, 44, 5, 6, 9, 11, 16]

Output

The output will be an integer greater than or equal to zero. The output should represent the minimum number of steps needed for the pointer to reach the end of the array. The output for the above-mentioned input will be:

3

Coding exercise

Implement the jumpGame(arr) function in the skeleton code below.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.