Find Last Element

import java.util.*;
public class Main {
public static int lastIndex(int a[], int n, int m)
{
if(n<=1)
return -1;
int ans;
if(a[n-1]==m)
return (n-1);
else
ans = lastIndex(a, n-1, m);
return ans;

}

public static void main(String[] args) {
	Scanner sc= new Scanner(System.in);
	int i,m;
//	System.out.println("Enter the length of array");
	int n= sc.nextInt();
	int a[] = new int[n];
	for(i=0; i<a.length; i++)
	{
		a[i] = sc.nextInt();
	}
//	System.out.println("Enter the target value");
    m = sc.nextInt();
    int ans = lastIndex(a,n,m);
    System.out.print(ans);

}

}
What is the test case 1? The output is the same but test case 1 is showing error.

Hi @Ashi,
Please remove the = in the first if statement … use (n<1) instead of (n<=1) and the code will run fine

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.