# java – Minimum number of jumps required

Saw this question while going through some String interview questions. I was not able to execute this since I was getting a Time Limit Exception on the website (Geeks for Geeks). I tried couple of test cases and seems to work fine. Would be grateful if someone can check the basic logic and tell me if I am missing something.

Given an array of integers where each element represents the max number of steps that can be made forward from that element. The task is to find the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then cannot move through that element.

``````public int minimumNumberOfJumps(int() arr) {
int currentLength = arr.length - 1, totalJumps = 0,
i = 0, value = 0;
while (true) {
value = arr(i);
if (value >= currentLength) {
return ++totalJumps;
} else if (value == 0) {
return -1;
}
i += value;
++totalJumps;
currentLength -= i;
}
}
``````

The test cases I tried were

``````int() testValue = {1, 4, 3, 2, 6, 7};
int() testValue1 = {1, 3, 5, 8, 9, 2, 6, 2, 6, 8, 9, 10, 4, 7, 10};
int() testValue2 = {1, 3, 5, 8, 9, 2, 6, 2, 6, 8, 9,}
``````

And thank you for taking the time to look at this 🙂