Minimum Jumps to Reach the End
Updated
Problem statement
You are given an integer array nums of length n. You start on index 0. From index i you can jump forward to any index j with i < j <= i + nums[i], as long as j is inside the array. So a jump may be shorter than nums[i], and if nums[i] is 0 you cannot jump from index i at all.
Return the fewest jumps needed to land on the last index, n - 1. The input is chosen so that the last index can always be reached. If n is 1, you already stand on the last index and the answer is 0.
Function
minJumps(nums: int[]) → intExamples
Example 1
nums = [2,4,1,1,1,1]return = 2From index 0 (value 2) you can jump to index 1 or 2. Jump to index 1: its value 4 reaches index 5, the last index. That is 2 jumps: 0 → 1 → 5.
One jump is not enough, because index 0 reaches at most index 2. Jumping the full length to index 2 would be a poor choice: it holds a 1, and from there you need 3 more jumps (2 → 3 → 4 → 5).
Example 2
nums = [2,3,0,1,4,0,0,0]return = 3The last index is 7. A best path is 0 → 1 → 4 → 7. Index 2 holds 0, so landing there is a dead end. From index 1 (value 3) you can reach index 2, 3 or 4; jump to 4. Its value 4 allows a jump of up to 4 steps, and a jump may be shorter, so a jump of 3 steps lands on index 7.
Two jumps are not enough: after one jump you stand on index 1 or 2, and from those the farthest you can reach is index 1 + 3 = 4. So the answer is 3.
Example 3
nums = [6]return = 0The array has one element, so you start on the last index. No jump is needed, so the answer is 0, whatever the value is.
Constraints
1 <= nums.length <= 10^40 <= nums[i] <= 100- The last index can be reached from index
0.
More Nutanix Online Assessment questions
- Good SubsequencesOA · Seen Jul 2026
- Min Cost to Connect All PointsOA · Seen Jun 2026
- Digit Changes for a Multiple of 9OA · Seen Aug 2025
- Gas Station Loop StartOA · Seen Aug 2025
- Number of Players (for MTS2)OA · Seen Oct 2024
- Work Schedules (for MTS2)OA · Seen Oct 2024
- Minimum Operations to Convert ArrayOA · Seen Apr 2024
- No Adjacent CharactersOA · Seen Apr 2024
- Build a BST From a ListOA · Seen Nov 2022
- Delete Even-Valued Nodes From a Linked ListOA · Seen Nov 2022
Similar questions at other companies
- Maximum Even-Sum Neighboring PairsDRW · OADRW interview questions
- Minimize Total Input Cost (for LTMS)Salesforce · OASalesforce interview questions
- Maximize Protected City PopulationAmazon · OA
- Network FormationPalantir · OAPalantir interview questions
- Maximum Even Subsequence SumExpedia · OAExpedia interview questions
- Maximum Even-Sum Adjacent Pairs in a Circular ArrayTesla · OATesla interview questions
- Fewest Fountains to Water the GardenDocuSign · OADocuSign interview questions
- Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions
- Split Array Largest SumAuxoAI · OA
- Maximum Income from Asset TradingJefferies · OA