intermediate · greedy · arrays · reachability
Can you hop to the last stone?
Problem
Stepping stones cross a stream in a row. You start on the first stone. The number on each stone is the longest hop you may make from it: from stone i with number h you can land on any stone from i + 1 to i + h. A 0 means you cannot move on from that stone.
Decide whether the last stone can be reached.
Constraints
- 1 ≤ n ≤ 32
- hop lengths from 0 to 9
Examples
[2, 3, 1, 1, 4] → true — Hop 1 to stone 1, then 3 to the last stone.
[3, 2, 1, 0, 4] → false — Every route lands on stone 3, which has hop 0.
Show the solution
Farthest reach
You never need to know which hops you take, only how far the reachable prefix extends. Each reachable stone can push that boundary further.
Why it is correct
Invariant: before visiting i, farthest is the largest index reachable using hops from stones 0…i − 1, and every index up to farthest is reachable (you can always hop shorter). If i > farthest, no stone before i reaches i, and no later stone is reachable either, so the answer is false. Otherwise i is reachable and farthest = max(farthest, i + h) restores the invariant. Finishing the loop means every index, including the last, was ≤ farthest.
Complexity
Time O(n), extra space O(1). One pass, one comparison and one max per stone.
For contrast: Mark stone 0 reachable; from each reachable stone, mark every stone within its hop. That costs O(n · h) time and O(n) space.
Edge cases
- One stone: already there (true).
- A 0 on the first stone with more stones: false.
- A 0 that can be jumped over is harmless.
Common mistakes
- Greedily taking the longest hop from each stone, which can land on a 0 that a shorter hop would avoid.
- Stopping when a stone is 0 even though it can be jumped over.
Step through it
The solution in four languages
Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.
namespace Bekadan.Dsa.Examples.Problems;
public static class ReachLastStone
{
/// <summary>From stone i you may hop 1..hops[i] stones forward. Can the last stone be reached from the first? O(n).</summary>
public static bool CanReach(int[] hops)
{
int farthest = 0;
for (int i = 0; i < hops.Length; i++)
{
if (i > farthest) return false;
farthest = Math.Max(farthest, i + hops[i]);
}
return true;
}
}
Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/ReachLastStone.cs
from collections.abc import Sequence
def can_reach(hops: Sequence[int]) -> bool:
"""From stone i you may hop 1..hops[i] stones forward. Can the last stone be reached from the first? O(n)."""
farthest = 0
for i, hop in enumerate(hops):
if i > farthest:
return False
farthest = max(farthest, i + hop)
return True
Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/reach_last_stone.py
package dev.bekadan.dsa.problems;
public final class ReachLastStone {
private ReachLastStone() {
}
/** From stone i you may hop 1..hops[i] stones forward. Can the last stone be reached from the first? O(n). */
public static boolean canReach(int[] hops) {
int farthest = 0;
for (int i = 0; i < hops.length; i++) {
if (i > farthest) {
return false;
}
farthest = Math.max(farthest, i + hops[i]);
}
return true;
}
}
Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/ReachLastStone.java
#pragma once
#include <algorithm>
#include <cstddef>
#include <span>
namespace bekadan::dsa::problems {
// From stone i you may hop 1..hops[i] stones forward. Can the last stone be reached from the first? O(n).
inline bool reach_last_stone(std::span<const int> hops) {
std::size_t farthest = 0;
for (std::size_t i = 0; i < hops.size(); ++i) {
if (i > farthest) return false;
farthest = std::max(farthest, i + static_cast<std::size_t>(hops[i]));
}
return true;
}
} // namespace bekadan::dsa::problems
Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/reach_last_stone.hpp