Last Occurrence - II

You are given an integer array nums and an integer target. Using recursion, return the largest zero-based index at which target occurs in nums. If target is not present, return -1.

Note:

A normal iterative solution will pass the test cases, but the goal of this problem is to solve it using recursion and strengthen your understanding of recursive calls and base cases.

Example 1:

Input: nums = [2, 3, 4, 5, 3], target = 3

Output: 4

Explanation: The target 3 occurs at indices 1 and 4, so its last occurrence is at index 4.

Example 2:

Input: nums = [2, -4, 4, 0, 10], target = 6

Output: -1

Explanation: The target 6 does not occur in nums.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [1, 3, 5, -4, 1], target = 1

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 100
  • -104 <= nums[i] <= 104
  • -104 <= target <= 104

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int lastOccurrence(vector<int>& nums, int target) {
// Your code goes here
}
};
Test Case

Input:

Nums
Target