709. Find Index of an Element Recursively

EasyRecursionRecursion

Given an integer array nums and an integer target, return the index of the FIRST occurrence of target in nums, or -1 if target is not present. You must implement this recursively, without using any loops.

Input: An integer array nums and an integer target.

Output: Return an integer index (smallest matching) or -1.

Examples

Example 1
Input: [10,20,30,40], 30
Output: 2
Explanation: 30 is at index 2.
Example 2
Input: [1,2,1,2,1], 2
Output: 1
Explanation: First 2 appears at index 1.
Example 3
Input: [5,6,7], 9
Output: -1
Explanation: 9 is not in the array.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →