Plz detect the error ..(last index by rec)

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int [] arr=new int[n];
for(int i=0;i<arr.length;i++){
arr[i]=sc.nextInt();
}
lastIndex(5,arr,77);
}
public static int lastIndex(int n,int[] arr,int nums){
if(n==0&&arr[n]!=nums){
return -1;
}
if(n<0){
return -1;
}
if(arr[n-1]==nums){
return n-1;
}
int Ans=lastIndex(n-1,arr,nums);
return Ans;
}
}

Hey @shubhamsagar, please use the online IDE to share the code as the formatting of the code changes here.

Now coming to the code by doing this you are returning as soon as the value is found as thus you aren’t traversing the entire array.

we need to make the following change:

Also, check the input format. You need to input the value which needs to be searched.
So this line is incorrect.

Hope it helps.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.