Please explain this ? not able to understand recursion :(

Take as input N, the size of array. Take N more inputs and store that in an array. Take as input M, a number. Write a recursive function which returns the last index at which M is found in the array and -1 if M is not found anywhere. Print the value returned.

@hg11110000 are you facing trouble with the implementation or the question itself? The question is asking you to perform a search on the array recursively. You have to return the last occurance of the element in the array (so the greatest index it can be found on)

implementation ! like i m not able to think the solution myself how to approach for it ???

@hg11110000 since it is not given that the arrays will be sorted or not, we can perform linear search only.
since we have to find the last occurance, it will be smarter to start searching from the ending.
in the function, there can be two scenarios, either we find the element or we dont.

  1. base case 1, condn : arr[n] == key
    this means that we have found the key, and because we are traversing from the back, it will be the last occurance only, so we return n.

  2. base case 2, condn : n < 0
    this implies that we have covered the entire array from n-1 to 0 and element was not found, so we return -1

  3. recursive case
    we searched the ith element, now we have to search in the rest of the array on the left so we can call
    f (arr, n - 1, key)