Minimum Number of Jumps to Reach End Given an array of integers where each element represents the maximum number of steps that can be jumped going forward from that element, write a function to return the minimum number of jumps to reach the end of the array (starting from the first element). If the end is not reachable, return -1.Constraints:NAExample:Sample Input:523114Sample Output:2Explanation:First line of input--> size of array (n)Then n lines of integers.Output--> From the above example ,the number of jumps needed to reach the end of array are 2Public Test Cases:# INPUT EXPECTED OUTPUT1 5231142
Question
Minimum Number of Jumps to Reach End Given an array of integers where each element represents the maximum number of steps that can be jumped going forward from that element, write a function to return the minimum number of jumps to reach the end of the array (starting from the first element). If the end is not reachable, return -1.Constraints:NAExample:Sample Input:523114Sample Output:2Explanation:First line of input--> size of array (n)Then n lines of integers.Output--> From the above example ,the number of jumps needed to reach the end of array are 2Public Test Cases:# INPUT EXPECTED OUTPUT1 5231142
Solution
Here is a Python solution for the problem:
def minJumps(arr, n):
if (n <= 1):
return 0
if (arr[0] == 0):
return -1
maxReach = arr[0]
step = arr[0]
jump =1
for i in range(1,n):
if (i == n-1):
return jump
maxReach = max(maxReach, i + arr[i])
step -= 1;
if (step == 0):
jump += 1
if(i >= maxReach):
return -1
step = maxReach - i;
return -1
arr = [5, 2, 3,
Similar Questions
You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:0 <= j <= nums[i] andi + j < nReturn the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1]. Example 1:Input: nums = [2,3,1,1,4]Output: 2Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.Example 2:Input: nums = [2,3,0,1,4]Output: 2 Constraints:1 <= nums.length <= 1040 <= nums[i] <= 1000It's guaranteed that you can reach nums[n - 1].
Java Program To Find Minimum No. of Jumps to Reach The End of an Array
Have the function ArrayChallenge(arr) take the array of numbers stored in arr and first determine the largest element in the array, and then determine whether or not you can reach that same element within the array by moving left or right continuously according to whatever integer is in the current spot. If you can reach the same spot within the array, then your program should output the least amount of jumps it took. For example: if the input is [2, 3, 5, 6, 1] you'll start at the spot where 6 is and if you jump 6 spaces to the right while looping around the array you end up at the last element where the 1 is. Then from here you jump 1 space to the left and you're back where you started, so your program should output 2. If it's impossible to end up back at the largest element in the array your program should output -1. The largest element in the array will never equal the number of elements in the array. The largest element will be unique.
Given an integer array A of length N. Where Ai is the cost of stepping on the ith stair.From ith stair, you can go to i+1th or i+2th numbered stair.Initially, you are at 1st stair find the minimum cost to reach Nth stair.Problem Constraints2 <= N <= 1051 <= Ai <= 104Input FormatThe first and only argument is an integer array A.Output FormatReturn an integer.Example InputInput 1:A = [1, 2, 1, 3]Input 2:A = [1, 2, 3, 4]Example OutputOutput 1:5Output 2:7Example ExplanationExplanation 1:1 -> 3 -> 4Explanation 2:1 -> 2 -> 4
is currently standing at stair 0 and wants to reach exactly the stair numbered C.The programmer can climb either H steps or 1 step in one move. Find the minimum number of moves required by the programmer to reach exactly the stair C.Input FormatThe first line of input will contain a single integer T, denoting the number of test cases.Each test case consists of a single line of input containing two space separated integers C and H denoting the number of stair the programmer wants to reach and the number of stairs the programmer can climb in one move.Constraints1 <= T <= 5001 <= C, H <= 100Output FormatFor each test case, output the minimum number of moves required by the programmer to reach exactly the stair numbered C.Sample Input 044 28 33 42 1Sample Output 02432Sample Input 13374 30588 71305 309Sample Output 17018305
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.