Jump Game - I

Greedy Algorithms Easy Easy

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.

Examples:

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]

Constraints

  • 1 <= nums.length <= 104
  • 0 <= nums[i] <= 105

Hints

  • Maintain a variable farthest to keep track of the farthest index you can reach as you iterate through the array. For each position i, calculate the maximum reach using farthest=max(farthest,i+nums[i]). If farthest ever becomes greater than or equal to the last index, return true.
  • If at any index i, the farthest reachable position is less than i, it means you cannot proceed further, and reaching the last index is impossible. Exit early and return false.

Company Tags

Shopify Mastercard Electronic Arts Byju's DoorDash Cloudflare HashiCorp Goldman Sachs Zynga Etsy IBM Optum PwC Epic Systems Splunk MongoDB Pinterest Boston Consulting Group Broadcom Zomato Riot Games HCL Technologies Seagate Technology Salesforce Stripe Google Microsoft Amazon Meta Apple Netflix Adobe