Jump Game
Keep one number, far: the farthest index we know we can reach. Walk i from left to right. If i is past far, it cannot be reached, so return false. Otherwise i is reachable and extends the block to i + nums[i], so set far = max(far, i + nums[i]).
Index i is reachable only if i β€ far, so one comparison between i and far decides whether the walk is stuck.
Problem
You start on index 0 of an array nums. Each number is the longest jump you can make from that index: from index i you may land on any index from i + 1 up to i + nums[i], as long as you stay inside the array.
Return true if you can reach the last index, and false otherwise.
An array of integers nums. nums[i] is the furthest forward jump from index i.
true if the last index can be reached from index 0, otherwise false.
Jump 1 step from index 0 to index 1 (its value is 3), then jump up to 3 steps from there to the last index.
No jump from index 0, 1, 2 or 3 gets past index 3, and index 3 holds 0. So index 4 is never reached.
- β’ 1 β€ nums.length β€ 10^4
- β’ 0 β€ nums[i] β€ 10^5
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck β each hint is counted.
- 1.
Do I need the exact route, or only whether some route reaches the end?
- 2.
If I stand on an index that holds 0, am I always stuck? What if I never have to land on it?
- 3.
Suppose every index up to some point is reachable. Which index is the furthest one I can be sure of?
- 4.
If index
iis reachable, which new indexes does it make reachable? - 5.
Can one number and one pass answer the question, without a table?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | From each index, try every jump length one by one | O(2^N) | O(N) |
| Better | For each index, check whether a reachable earlier index can jump to it | O(NΒ²) | O(N) |
| Optimal | Scan left to right and keep the farthest index you can reach so far | O(N) | O(1) |
Why does this belong to this pattern?
- βjump at most nums[i] stepsβFrom index `i` you can land on any of up to `nums[i]` spots after it. Every jump goes right, never left.
- βreach the last indexβWe only need a yes or no, so the reachable block is enough. We do not need the routes.
- βstart on index 0βThe reachable indexes start at 0 and grow to the right with no holes.
This is the anchor of the reachability sub-pattern: the whole answer depends on one number, the farthest index reached so far. Jump Game II keeps the same frontier but counts rounds. Jump Game III breaks the rule, because a jump can also go left, so the reachable indexes are no longer one block that starts at the beginning.
Don't make this mistake
A zero is a dead end only if the walk has to stop on it. On [2, 0, 0] you jump 2 steps from index 0 straight to index 2, the last index, so the answer is true. The wrong check returns false at index 1. What matters is whether index i is inside the block (i β€ far), not whether its value is 0.
After the update, far is at least i + nums[i], which is at least i. So i > far can never be true, and the check never fires. On [3, 2, 1, 0, 4] the wrong version returns true, but the answer is false: index 4 is past far = 3.
With nums = [0] you already stand on the last index, so the answer is true. The loop starts at index 0, which is also the last index, so it returns true without any jump.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Greedy translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.