You are given an array of integers and a target element x. Your task is to find the index of the first occurrence of x in the array using recursion only. You are not allowed to use any loops.
If the element exists, return its index (0-based). If it is not present in the array, return -1.
Example 1
Input: arr[] = [ 1 4 5 8 3 5], x = 5
Output: 2
Explanation: Since the array is 1 4 5 8 3 5 and the target element is 5, the first occurrence of 5 appears at index 2 (0-based indexing).
Example 2
Input: arr[] = [ 4 8 9 6], x = 1
Output: -1
Explanation: Since the array does not contain the target element 1, there is no occurrence of it, so we return -1.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1 2 3 4 5 3
2
10 20 30 40 10
0
-99 -11 -99
0