2nd testcase is failing

problem :
FOUND AT LAST
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.

Input Format:
There will be three lines of input:

N - the size of the array
N space separated integers that make up the array
M
Constraints:
1 < N < 1000
-10^9 < i,M < 10^9 , where i is any number within the array

Output Format
For each case, print the integer value of the last index that M is found at within the given array.
If it is not found, print ‘-1’ (without the quotes).

code:
https://ide.codingblocks.com/s/52671

Hi Gaurav,
Initialise maxIndex with -1, when element is not found your code is giving wrong ans.

Note: tip to improve your code : you can create function as void if you are using global variable or create local instead then return value.

HIT LIKE IF YOU GET IT :stuck_out_tongue:
CHEERS :smiley:

1 Like

it is working now but I am not able to understand how initialising value of maxIndex with -1 makes it working

when an element is not found your code is giving wrong ans.
when an element is not found, do a dry run you’ll see maxIndex will return a garbage value.
Initializing it will return -1.

HIT LIKE IF YOU GET IT :stuck_out_tongue:
CHEERS :smiley:

oh got it thank you…