Jump Game - I

Greedy Algorithms Easy Easy
  • This problem's underlying concept is applied in routing and networking protocols
  • For example, the Internet Protocol (IP) uses routing algorithms based on the shortest path, which is similar conceptually to the array jump problem
  • These protocols need to calculate the shortest or most efficient route for data packets to reach their destination, making sure data can indeed ‘jump’ from one node to another until it reaches its final destination, or return 'false' if it cannot
  • The problem's notion is also central in video game development
  • For instance, in pathfinding logic for non-playable characters, algorithms evaluate if a character can move from one point to another by 'jumping' through specified navigation points, much like jumping through indices of an array

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