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

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. 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...
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
The program shall take two integer arrays as input. Each array represents a non-negative number. Each...
The program shall take two integer arrays as input. Each array represents a non-negative number. Each element in the array is one digit however when all digits in the array are combined a number is formed. See example below: int Array1 = {4,5,6,7} represents number 7654 int Array2 = {2,3,4,5} represents number 5432 You will add the two numbers i.e., 7654 + 5432 = 13086 and store it in a new array similar to how the numbers were stored earlier...
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...
In python write a program that gets a list of integers from input, and outputs non-negative...
In python write a program that gets a list of integers from input, and outputs non-negative integers in ascending order (lowest to highest). Ex: If the input is: 10 -7 4 39 -6 12 2 the output is: 2 4 10 12 39 For coding simplicity, follow every output value by a space. Do not end with newline
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!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT