If you are preparing for software engineering interviews, you have almost certainly encountered LeetCode 55: Jump Game. It is a classic array problem that tests your ability to track state while iterating through data.
In this post, we will break down the Jump Game problem, understand the core intuition needed to solve it, and walk through a highly optimal, clean C++ solution. We will completely drop the heavy algorithm terminology and focus purely on the core logic and intuition.
The Problem Statement
Let's quickly review the rules of the game:
You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
Your goal is simple: Return true if you can reach the last index, or false otherwise.
Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
The Intuition: Tracking Your Maximum Reach
The easiest way to solve this problem is to imagine yourself walking through the array step-by-step. You don't need to map out the exact sequence of jumps; you just need to know if reaching the end is possible.
To do this, we can maintain a single piece of information as we walk: the absolute furthest index we can currently reach.
Here is the game plan:
- Start at index 0. Your current reach is based on the value at index 0.
- Walk forward one step at a time.
- At each step, ask yourself: "Does the jump available at my current position allow me to reach further than my previous maximum reach?" If so, update your maximum reach.
- If your current position ever catches up to your maximum reach and the jump value there is 0, you are stuck. You cannot move forward, so you return false.
- If your maximum reach equals or exceeds the last index of the array, you win! Return true.
The Optimal C++ Code
Here is how we translate that intuition into clean, efficient C++ code:
class Solution {
public:
bool canJump(vector<int>& nums) {
int range = 0; // The furthest index we can currently reach
int pos = 0; // Our current position in the array
// We can only keep walking if our current position is within our reach
while(pos <= range){
// If our reach is already at or past the last index, we win!
if(range >= nums.size() - 1) return true;
// Update our maximum reach from the current position
range = max(range, nums[pos] + pos);
// Take one step forward
pos++;
}
// If the loop ends naturally, it means pos > range, so we got stuck.
return false;
}
};
Code Walkthrough
Let's break down exactly what makes this code work so well:
- The Setup: We initialize
range = 0andpos = 0. We are standing at the starting line, and we haven't unlocked any jumps yet. - The while Loop (
pos <= range): This condition acts as our safety check. We are only allowed to advanceposif we actually have the jumping power to be there. If we hit a series of zeros andpossurpassesrange, the loop breaks. - The Early Exit: We don't need to process the whole array if we already have enough jumping power to cross the finish line.
if(range >= nums.size() - 1)saves valuable processing time. - Updating the Range: This is the core engine of the algorithm. At every single step, we calculate
nums[pos] + pos(our current index + how far we can jump from here). If this new reach is better than our old range, we update it viamax().
Complexity Analysis
This approach is highly optimized for performance:
- Time Complexity: O(N) — We iterate through the array a maximum of one time. The
pospointer simply walks from left to right, meaning the time scales linearly with the size of the input. - Space Complexity: O(1) — We are only storing two integer variables (
rangeandpos). No matter how large the input array grows, our memory usage remains perfectly constant.
Conclusion
The Jump Game teaches us a valuable lesson in array traversal: you don't always need to simulate every possible path. By simply tracking your "maximum boundaries" as you iterate, you can solve complex-looking problems with elegant, linear-time code.
Track Your DSA Progress — It's Free
Stop solving random questions. Start with the right 206 questions across 16 patterns — structured, curated, and completely free.