Quick revision
The short answer
The fewest jumps problem is a greedy problem that is really a breadth-first search in rounds. Walking the array once, we track currentEnd, the last index reachable with the jumps made so far, and farthest, how far one more jump could reach; whenever i reaches currentEnd we count a jump and move currentEnd to farthest. It takes O(n) time and O(1) space, and returns -1 when the reach stops growing before the end.
The solution
What it costs
| Approach | Time | Extra space | Correct? |
|---|---|---|---|
| Try every leap recursively | exponential | O(n) call stack | yes |
| Table of fewest jumps per index | O(n^2) worst case | O(n) | yes |
| Always leap as far as possible | O(n) | O(1) | no |
| Greedy scan by rounds | O(n) | O(1) | yes |
What to remember
We found the fewest jumps by thinking in rounds instead of routes. The stones reachable with k jumps form one unbroken stretch, so a single walk from left to right can track where the current round ends and how far the next one reaches, counting a jump each time a round is used up. That is O(n) time and O(1) space, with -1 when farthest stops growing. The clue to remember is a fewest-moves question where each position reaches a whole unbroken range: the breadth-first search collapses into a greedy scan of the farthest reach.
1Crossing a river on stepping stones
Imagine a row of stepping stones across a river. On each stone someone has painted a number, and the number tells you the longest leap you are allowed from that stone. A 2 means you may leap one stone ahead or two stones ahead, whichever you like; a 0 means you cannot leave that stone at all. You start on the first stone, and you want to reach the last one in as few leaps as possible.
That is exactly our problem. The array is the row of stones, the index is the stone's position, and the value at index i is the longest jump from there, so from index i we may land anywhere from i + 1 up to i + a[i]. We must return the fewest jumps that take us from index 0 to the last index. Notice what we are not asked: we do not have to list the route, only count its jumps.
This problem has a reputation for being tricky, so let me say plainly that it is smaller than it looks. The final solution walks the array once from left to right with three whole numbers in hand. What makes it feel hard is that the obvious rule, leap as far as you can, gives the wrong answer, and we will see exactly why before we fix it.
Let us keep one example for the whole article: [2, 3, 1, 1, 4], five stones, so the last index is 4. Work it on paper with me. From index 0 the value is 2, so the first leap lands on index 1 or index 2. If we land on index 1, its value 3 lets us reach index 4 in one more leap, so two jumps are enough. Could one jump do it? No, because from index 0 we can reach at most index 2. So the answer is 2.
Each cell is a stone; its value is the longest leap allowed from it. The highlighted stones are one route that uses the fewest jumps.
- route
- 0 -> 1 -> 4
- answer
- 2 jumps
1From index 0 (value 2) we may land on index 1 or 2. Landing on index 1 (value 3) lets the second jump reach index 4, so 2 jumps are enough.
What to assume
- Are the values ever negative?
- We assume not.
- Is the last index always reachable?
- Not necessarily: in [3, 2, 1, 0, 4] every route lands on the 0 at index 3 and gets stuck, so we return -1 for that case.
- What if the array has one element?
- Then we already stand on the last index and need 0 jumps. And since the array may hold a hundred thousand numbers, we should aim for a single pass.
A careful candidate asks a few questions first. Are the values ever negative? We assume not. Is the last index always reachable? Not necessarily: in [3, 2, 1, 0, 4] every route lands on the 0 at index 3 and gets stuck, so we return -1 for that case. What if the array has one element? Then we already stand on the last index and need 0 jumps. And since the array may hold a hundred thousand numbers, we should aim for a single pass.
Your turn
Solve it before you read on. Above is one example with the result expected for it. Under What to assume are the points this solution takes as given. Say your approach aloud or type it below, and write the code in your own editor. Then take one hint at a time and compare.
2How to recognise a greedy problem hidden inside a shortest-route question
A beginner reading this for the first time usually thinks of trying routes. From each stone we have a choice of landings, from each landing more choices, and somewhere in that bush of choices is the shortest route. That instinct is correct, and it is the brute force we will write next. The question is how to avoid walking every branch of the bush.
So let us take the clues in the question one at a time. The first clue is the word fewest. Fewest jumps is a shortest-route question in which every jump costs the same, one. The classic way to find such a route is to explore in rounds: first everything reachable with one jump, then everything reachable with two, and so on. This round-by-round exploration is called breadth-first search, and the round in which we first reach the last stone gives the fewest jumps.
The second clue is the phrase maximum jump. From index i we can land on any index from i + 1 to i + a[i], a whole unbroken stretch of stones, not scattered ones. That matters a great deal. If every round starts from a stretch of stones and each stone reaches a stretch, then the stones reachable in the next round also form one unbroken stretch. We will never need a queue or a visited set; we only need to know where each stretch ends.
The third clue is the array itself, read from left to right. Each stretch begins just after the previous one ends, so a single walk from left to right passes through the rounds in order, and while walking through one round we can work out how far the next round reaches.
Together these clues point to the greedy pattern. Greedy means that at each step we make one choice that looks best right now and never go back to reconsider it. Here the choice is how far the next jump could possibly take us, and we keep only the best. A greedy method is only trustworthy when we can explain why the local choice is safe, and the rounds give us that explanation. The rule to carry to new problems is that when every move costs the same and each position reaches a whole unbroken range, the breadth-first search collapses into a greedy scan that only tracks the farthest reach.
Two neighbours look similar. Dynamic programming, which stores the fewest jumps for every index and builds each answer from earlier ones, is correct here but can be quadratic when the values are large. And there is a false greedy, always leaping as far as you can, which we will meet and defeat in the section after next.
3The honest first attempt, trying every leap
The honest first attempt tries every possibility, and that is a good place to start, because it is clearly correct. Stand on stone i. If it is the last stone, or past it, we need 0 more jumps. Otherwise try each leap length from 1 to a[i], ask the same question about the stone we land on, and take the smallest answer plus one for the leap we just made. A method that answers a question by asking the same question about a smaller part is called recursive.
Let us run it by hand on [2, 3, 1, 1, 4]. From index 0 we try landing on 1 and on 2. From index 1 we try 2, 3 and 4; index 4 is the end, so that branch costs 0, and index 1 can finish in 1 jump. From index 2 we try only 3, and from 3 only 4, so index 2 needs 2 jumps. The best from index 0 is therefore 1 + min(1, 2) = 2, the right answer.
Now let us count. On our five stones the method is called 11 times. That sounds harmless, but look closely at the hand calculation: we worked out the answer for index 3 more than once, once on the way through index 1 and again on the way through index 2. On the eleven-stone array [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9], which our final program also tests, the same method is called 1,412 times. Each extra stone can multiply the number of routes, so the time grows exponentially, roughly doubling or worse with every stone added. The space is O(n), the depth of the waiting calls.
So the waste is plain to see. The fewest jumps from a given stone never changes, yet the brute force recomputes it every time a different route arrives there. Writing each answer in a table the first time we compute it fixes that, and gives an O(n squared) dynamic programming method, because each stone may update up to n others. That is better, but we can do better still by thinking about rounds instead of routes.
4From routes to rounds, one observation at a time
Let us start from the river again. Instead of following one route, picture a crowd of people starting together on stone 0, each person free to leap any allowed distance. After one leap the crowd is spread over every stone one leap can reach. On our example that is stones 1 and 2, because the value 2 at index 0 allows a leap of one or two. Call that round 1. Before any leap, round 0 is just stone 0.
Now a beginner may reasonably ask why we need a crowd at all. Why not simply take the longest leap every time? It sounds sensible, because a long leap covers ground quickly. Let us try it on the example and see what happens.
Follow the rule of always taking the longest leap and count the jumps. Compare with the 2 jumps of the best route.
- jumps
- 1
- route
- 0 -> 2
1From index 0 the longest leap is 2, so the rule lands on index 2. It skips index 1, which is the stone that could have reached the end.
- jumps
- 2
- route
- 0 -> 2 -> 3
2Index 2 holds 1, so the longest leap from here is only 1, and we land on index 3. The far landing gave us a weak stone.
- jumps
- 3
- route
- 0 -> 2 -> 3 -> 4
- best
- 2
3Index 3 holds 1, so one more leap reaches index 4. The rule used 3 jumps, one more than the best route 0 -> 1 -> 4.
The rule landed on index 2, whose value is only 1, and needed three jumps. The lesson is that landing far is not the same as landing on a stone that reaches far. The right question for the first jump is not where it lands, but which of the stones it could land on lets the next jump go furthest. Index 1 is nearer, yet it reaches index 4.
So let us go back to the crowd and ask what round 2 looks like. From stone 1 the crowd can reach as far as index 1 + 3 = 4, and from stone 2 as far as 2 + 1 = 3. So after two leaps the crowd covers every stone up to index 4. We only had to know the farthest reach among the stones of round 1. Round 2 is the stretch from just after round 1 ends, index 3, up to that farthest reach, index 4. And since index 4 is the last stone, two leaps are enough.
This gives us the method. We keep currentEnd, the last index of the current round, and farthest, the furthest index any stone seen so far can reach. We walk i from left to right. At each stone we update farthest with i + a[i]. When i reaches currentEnd, we have seen every stone of this round, so the next round ends at farthest: we count one jump and set currentEnd to farthest. The number of jumps when currentEnd first covers the last index is the answer.
Why is this the fewest? Because round k contains exactly the stones that can be reached with k jumps and no fewer. If the last stone first appears in round 2, no route of one jump reaches it, so 2 is the minimum. The greedy choice of keeping only the farthest reach is safe, because a round is an unbroken stretch, and the farthest end is all we need to describe it.
There is one question left. What if the crowd cannot move? Suppose at the end of a round farthest still equals currentEnd. Then no stone in the round reaches beyond it, the next round would be empty, and the end can never be reached, so we return -1. And we loop only up to the second-to-last index, because once we stand on the last stone no further jump is needed.
5Watching the rounds on the example
The picture below plays the loop on [2, 3, 1, 1, 4]. Under the cells, i marks the stone we stand on, end marks currentEnd, the last stone of the current round, and far marks the farthest stone the next jump could reach. Stones marked as done belong to earlier rounds, and the compared stones are the round still being explored or about to be entered.
Watch for the two kinds of step. Most steps only update farthest. Only when i reaches end does a jump get counted, and then end moves forward to far.
Cells marked done belong to earlier rounds; compared cells are the round being explored or about to be entered. i is the stone we stand on.
- jumps
- 0
- currentEnd
- 0
- farthest
- 0
- round 0
- index 0
1Before the loop we stand on index 0 with no jumps made. The current round is just index 0, so
currentEndis 0, and farthest is 0 too.- jumps
- 0
- currentEnd
- 0
- farthest
- 2
- check
- 0 + 2 = 2
2At i = 0 the value is 2, so one more jump from here reaches index 0 + 2 = 2. farthest becomes 2.
- jumps
- 1
- currentEnd
- 2
- farthest
- 2
- round 1
- indexes 1 to 2
3i has reached
currentEnd, so round 0 is used up and we must jump. jumps becomes 1, and the new round is indexes 1 to 2, socurrentEndbecomes farthest, 2.- jumps
- 1
- currentEnd
- 2
- farthest
- 4
- check
- 1 + 3 = 4
4At i = 1 the value is 3, so a jump from here reaches 1 + 3 = 4, the last index. farthest grows to 4. We do not jump yet, because index 2 is still in this round.
- jumps
- 1
- currentEnd
- 2
- farthest
- 4
- check
- 2 + 1 = 3, keep 4
5At i = 2 the value is 1, which reaches only 2 + 1 = 3. That is less than 4, so farthest stays 4. Remembering the best of the round is the whole trick.
- jumps
- 2
- currentEnd
- 4
- farthest
- 4
- round 2
- indexes 3 to 4
6i has reached
currentEndagain, so round 1 is used up. jumps becomes 2 andcurrentEndbecomes 4. That covers the last index, so the loop breaks.- jumps
- 2
- currentEnd
- 4
- farthest
- 4
- answer
- 2 jumps
7The method returns 2. One route that uses two jumps is 0 -> 1 -> 4: the first jump lands on index 1, the stone in round 1 that reached farthest.
Let us read the important frames back. In the third frame, i is 0 and so is currentEnd, so round 0 is finished. We count the first jump, and round 1 becomes indexes 1 to 2. Notice that we have not decided which stone the jump lands on; we only know how far it can reach.
In the fourth and fifth frames, we walk through round 1. Index 1 pushes farthest to 4, and index 2, which only reaches 3, leaves it at 4. This is where the false greedy went wrong: it chose index 2 because it was further away, while the scan quietly remembers that index 1 is the better take-off point.
In the sixth frame, i reaches currentEnd = 2, so round 1 is used up. We count the second jump and move currentEnd to 4, which is the last index, so the loop breaks and we return 2. The route 0 -> 1 -> 4 in the last frame is one route that matches this count.
6The complete Java program
Here is the full program. minJumps takes the array and returns the fewest jumps, or -1 if the last index cannot be reached. main runs our example, a longer array, the stuck case, a single stone and a row of ones, and each comment shows what that line prints.
Let us read minJumps in the order it runs. jumps starts at 0. currentEnd starts at 0, because with zero jumps the only stone we can be on is index 0. farthest also starts at 0, since we have not yet looked at any stone.
The loop runs i from 0 up to the second-to-last index. Why not the last index too? Standing on the last stone needs no jump, and with a single stone, [0], the test at index 0 would find farthest equal to currentEnd and wrongly return -1. Stopping one early means the loop does not run at all for one stone.
Inside the loop, the first line updates farthest with i + a[i], the reach of the stone we stand on. Then, if i == currentEnd, we have finished the current round. If farthest has not moved beyond currentEnd, no stone in the round can go further, so we return -1. Otherwise we count the jump with jumps++ and start the next round with currentEnd = farthest.
The last line inside the if checks whether the new round already covers the last index, and breaks out early if so. It does not change the answer, but it saves the remaining steps. After the loop we return jumps. So main prints 2, 3, -1, 0 and 3. The last one is [1, 1, 1, 1], where each round is a single stone and every jump moves one place.
7How much it costs, and the cases at the edges
Let us count on the example first. The loop looked at indexes 0, 1 and 2 and then broke, three steps, each a few comparisons. For n stones, i visits each index at most once and does constant work there, so the time is O(n). We keep three whole numbers, jumps, currentEnd and farthest, whatever the length, so the extra space is O(1), read as constant.
| Approach | Time | Extra space | Correct? |
|---|---|---|---|
| Try every leap recursively | exponential | O(n) call stack | yes |
| Table of fewest jumps per index | O(n^2) worst case | O(n) | yes |
| Always leap as far as possible | O(n) | O(1) | no |
| Greedy scan by rounds | O(n) | O(1) | yes |
Now the edges, one by one. A single stone, [0], makes the loop run zero times, because there is no second-to-last index, and we return 0. The stuck case [3, 2, 1, 0, 4] goes like this: round 1 ends at index 3, and while scanning indexes 1, 2 and 3 farthest never passes 3; at i = 3 we have i == currentEnd and farthest == currentEnd, so we return -1.
A 0 that can be leapt over is harmless, as in [2, 0, 1]: farthest is already 2 before we reach the 0, so the end is in range after one jump. Very large values are harmless as well, because farthest may run past the last index and the break handles it. If values could be close to the largest int, i + a[i] could overflow, and we would compute it as a long.
8Follow-up questions
How would you only check whether the end can be reached at all?
That simpler question needs just farthest. We walk the array, and if we ever stand on an index greater than farthest, no leap reaches that stone, so we return false. Otherwise we update farthest with i + a[i], and if it reaches the last index we return true. It is the same left-to-right walk, O(n) time and O(1) space, with no rounds to count.
How would you return the route itself, not only the number of jumps?
While scanning a round we also remember which index produced the current farthest reach. When the round ends, that index is the stone from which the next jump should take off, so we write it down. On our example, round 0 gives take-off stone 0 and round 1 gives take-off stone 1, and with the last index we get the route 0 -> 1 -> 4. It stays O(n) time, plus the space for the route.
Why is this called greedy if it behaves like breadth-first search?
Both names are fair. The idea comes from breadth-first search, which explores in rounds, but because each round is one unbroken stretch, we never store the stones of a round. At each step we greedily keep the single best number, the farthest reach, and throw the rest away. Explaining that link to the interviewer is the best way to show the greedy choice is safe.
9Conclusion
We found the fewest jumps by thinking in rounds instead of routes. The stones reachable with k jumps form one unbroken stretch, so a single walk from left to right can track where the current round ends and how far the next one reaches, counting a jump each time a round is used up. That is O(n) time and O(1) space, with -1 when farthest stops growing. The clue to remember is a fewest-moves question where each position reaches a whole unbroken range: the breadth-first search collapses into a greedy scan of the farthest reach.
Open with
Each value is the longest jump from that index, and I want the fewest jumps from index 0 to the last index, returning -1 if it cannot be reached.
Then, if they want more
Trying every leap recursively is correct but exponential, and a table of the fewest jumps per index works in O(n squared). Leaping as far as possible each time is wrong: on 2, 3, 1, 1, 4 it lands on index 2 and needs three jumps, while landing on index 1 needs two.
Since every jump costs one, I think of breadth-first search in rounds, and because each index reaches an unbroken range, every round is an unbroken stretch of indexes. So I walk the array once with currentEnd, the end of the current round, and farthest, the furthest any index so far can reach.
At each index I update farthest with i plus a of i. When i reaches currentEnd, the round is finished, so I count a jump and set currentEnd to farthest; if farthest did not move past currentEnd, I return -1. I loop only to the second-to-last index, because standing on the last one needs no jump.
It is O(n) time and O(1) space, and a single element needs zero jumps.
About this answer
- Last updated
- 14 November 2026
- Written for
- Java 21, using only java.util
- Checked
- The Java solution was compiled and run, and each printed result matches the comment beside it. The pictures are checked against the format when the site is built. The explanation around the code is a study note: confirm anything critical against the documentation below. What the checks cover
- Where the question comes from
- Public interview write-ups, reworded by Scrivio; counted 23 times in that collection. How it is counted
- Official documentation
- java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Each array element gives the maximum jump from that position. Find the fewest jumps...": https://scrivio-interview-library.netlify.app/data-structures/solutions/each-array-element-gives-the-maximum-jump-from-that-position-fin-d3357e/