Question

In: Computer Science

Input: An array of non-negative integers, where each element in the array represents your maximum jump...

Input: An array of non-negative integers, where each element in the array represents your maximum jump length at that position.

Output: A boolean value if you are able to reach the last index starting if you start at the first spot in the array.

[Please write a recursion function!]

Example 1:

  • Input: [2,4,1,2,4,1]
  • Output: True (Ex. 0 to 1 to 5 or 0 to 2 to 3 to 5)

Example 2:

  • Input: [3,2,1,0,4]
  • Output: false (You will always arrive at, and get stuck at, index 3)

Solutions

Expert Solution

Dear Learner,

Here is the code for your answer,

def jumpfunction(i,A,iteR=0): #the i is the beginning index to start from
   #we will take an additional parameter so as to stop the function from going in infinite loop
   if iteR<len(A): #checking if we have exceeded the max number of iterations
       if(i==(len(A)-1)): #we are checking if we have reached the end of the list
           return bool(True) #returning the result
       else:
           return jumpfunction(i+A[i],A,iteR+1) #recursive call to the remaining part of the array
   else:
       return bool(False) #returning false as we have crossed the maximum value

A = [3,2,1,0,4]
result = bool(jumpfunction(0,A)) #calling the function and storing the result in 'result' variable
print(result)

SCREENSHOT OF CODE :-

CORRESPONDING OUTPUT:-

FOR ANOTHER INPUT

Note that I have taken static values for the array, but the code will be the same for dynamic values as well.

I hope I have answered the question the way you were expecting. If you still have any doubts or want any other explanation, feel free to ask us in the comment section.

Please leave a like if this was helpful.

Thanks,

Happy Studying.


Related Solutions

write a code for given an array of integers where wachelement represents the maximum number...
write a code for given an array of integers where wach element represents the maximum number of jumps to reach the end of the array(starting from the first element) if an element O,then no jump can be made from that element if it is not possible to reach the end then output in c
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and...
Write a function ‘sort1’ that takes in an array of non-zero positive integers as input and returns a second vector that contains only the odd numbers. It will return zero if all elements are even. Use error-traps to check against probable errors in user input. In case of an error, it will return NaN. You are allowed to use Matlab built-in function round(). Check your code with the following arrays: >> y1 = [18, -5, 89, -7, 4, 10, 12,...
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.
Given an array of integers, delete each element from the array which is a multiple of 5, and display the rest of the array.Input:    6    2 3 4 11 22 320    where:First line represents the number of elements in the array.Second line represents the elements in the array.Output:    2 3 4 11 22Explanation: Element of the array 320 is the only one in the array which is a multiple of 5, so it is removed from the array.Assumptions:Array can be of size...
How would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array.
JAVA ProgrammingHow would you take a given array of non-repeating random integers and sort every 5th element. Meaning index 0 is the smallest element in the array. index 4 is the 5th smallest element in the array, index 9 is the 10th smallest element in the array and so on...- this array could be small (like 5 indexes) or large (like 100,000 indexes).- all other numbers do not change position
main() gets user input for size and elements of arr[]. . Check each element for negative,...
main() gets user input for size and elements of arr[]. . Check each element for negative, positive and zero and create neg[], pos[] and zer[] accordingly in main() only.
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1...
Creates a 100-element array, either statically or dynamically Fills the array with random integers between 1 and 100 inclusive Then, creates two more 100-element arrays, one holding odd values and the other holding even values. Prints both of the new arrays to the console. In C++. Thank you!
Algorithm1 prefixAverages1(X, n) Input array X of n integers Output array A of prefix averages of...
Algorithm1 prefixAverages1(X, n) Input array X of n integers Output array A of prefix averages of X A ← new array of n integers for i ← 0 to n − 1 do s ← X[0] for j ← 1 to i do s ← s + X[j] A[i] ← s / (i + 1) return A ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Algorithm2 prefixAverages2(X, n) Input array X of n integers Output array A of prefix averages of X A ← new array of...
One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
Let A be an array of non-decreasing N integers. Write an algorithm that returns the number...
Let A be an array of non-decreasing N integers. Write an algorithm that returns the number of pairs of integers in A that are equal. Analyze your algorithm thoroughly. Your analysis should include a thorough examination of both the best and the worst-case scenarios. This includes a description of what the best and worst cases would look like. You should include both space and time in your analysis, but keep in mind that space refers to “extra space,” meaning in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT