Given an array of integers nums, each element in the array represents the maximum jump length at that position. Initially starting at the first index of the array, determine if it is possible to reach the last index. Return true if the last index can be reached, otherwise return false.
Input : [2, 3, 1, 1, 4]
Output : true
Explanation : We can simply take Jump of 1 step at each index to reach the last index.
Input : [3, 2, 1, 0, 4]
Output : false
Explanation : No matter how you make jumps you will always reach the third index (0 base) of the array.
The maximum jump of index three is 0, So you can never reach the last index of array.
Input : [5, 3, 2, 1, 0]
Possible to Reach
Not possible to Reach
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// To determine if last index is reachable
bool canJump(vector<int>& nums) {
// Initialize maximum index
int maxIndex = 0;
// Iterate through each index of the array
for (int i = 0; i < nums.size(); i++) {
/*If the current index
is greater than the
maximum reachable index
it means we cannot move
forward and should
return false*/
if (i > maxIndex) {
return false;
}
/* Update the maximum index that can be
reached by comparing
the current maxIndex with the sum
of the current index and
the maximum jump from that index*/
maxIndex = max(maxIndex, i + nums[i]);
}
/* If we complete the
loop, it means we
can reach the
last index*/
return true;
}
};
int main() {
vector<int> nums = {4, 3, 7, 1, 2};
cout << "Array representing maximum jump from each index: ";
for (int i = 0; i < nums.size(); i++) {
cout << nums[i] << " ";
}
cout << endl;
Solution solution;
bool ans = solution.canJump(nums);
if (ans) {
cout << "It is possible to reach the last index." << endl;
} else {
cout << "It is not possible to reach the last index." << endl;
}
return 0;
}
import java.util.*;
class Solution {
// To determine if last index is reachable
public boolean canJump(int[] nums) {
// Initialize maximum index
int maxIndex = 0;
// Iterate through each index of the array
for (int i = 0; i < nums.length; i++) {
/* If the current index
is greater than the
maximum reachable index
it means we cannot move
forward and should
return false */
if (i > maxIndex) {
return false;
}
/* Update the maximum index that can be
reached by comparing
the current maxIndex with the sum
of the current index and
the maximum jump from that index */
maxIndex = Math.max(maxIndex, i + nums[i]);
}
/* If we complete the
loop, it means we
can reach the
last index */
return true;
}
public static void main(String[] args) {
int[] nums = {4, 3, 7, 1, 2};
System.out.print("Array representing maximum jump from each index: ");
for (int num : nums) {
System.out.print(num + " ");
}
System.out.println();
Solution solution = new Solution();
boolean ans = solution.canJump(nums);
if (ans) {
System.out.println("It is possible to reach the last index.");
} else {
System.out.println("It is not possible to reach the last index.");
}
}
}
class Solution:
# To determine if last index is reachable
def canJump(self, nums):
# Initialize maximum index
max_index = 0
# Iterate through each index of the array
for i in range(len(nums)):
# If the current index is greater than the
# maximum reachable index it means we cannot move
# forward and should return false
if i > max_index:
return False
# Update the maximum index that can be
# reached by comparing the current maxIndex with the sum
# of the current index and the maximum jump from that index
max_index = max(max_index, i + nums[i])
# If we complete the loop, it means we can reach the last index
return True
# Example usage
if __name__ == "__main__":
nums = [4, 3, 7, 1, 2]
print("Array representing maximum jump from each index: ", end="")
for num in nums:
print(num, end=" ")
print()
solution = Solution()
ans = solution.canJump(nums)
if ans:
print("It is possible to reach the last index.")
else:
print("It is not possible to reach the last index.")
class Solution {
// To determine if last index is reachable
canJump(nums) {
// Initialize maximum index
let maxIndex = 0;
// Iterate through each index of the array
for (let i = 0; i < nums.length; i++) {
/* If the current index
is greater than the
maximum reachable index
it means we cannot move
forward and should
return false */
if (i > maxIndex) {
return false;
}
/* Update the maximum index that can be
reached by comparing
the current maxIndex with the sum
of the current index and
the maximum jump from that index */
maxIndex = Math.max(maxIndex, i + nums[i]);
}
/* If we complete the
loop, it means we
can reach the
last index */
return true;
}
}
// Example usage
const nums = [4, 3, 7, 1, 2];
console.log("Array representing maximum jump from each index: " + nums.join(" "));
const solution = new Solution();
const ans = solution.canJump(nums);
if (ans) {
console.log("It is possible to reach the last index.");
} else {
console.log("It is not possible to reach the last index.");
}
Q: What happens if the array contains very large jump lengths?
A: Large jump lengths don’t impact the algorithm because it only cares about the maximum reachable index at each step, not the specific jump sizes.
Q: What if the array contains negative or non-integer values?
A: The problem assumes the array contains only non-negative integers. Negative or non-integer values would require additional validation and adjustments to the logic.
Q: What if you need to find the minimum number of jumps to reach the last index?
A: Extend the greedy approach by tracking the end of the current jump range and counting the number of jumps. Increment the jump count whenever the current index exceeds the range of the current jump.
Q: What if you want to return the indices of the path taken?
A: Modify the greedy solution to store the path by keeping track of the index from which each jump was made. Backtrack from the last index to reconstruct the path.