Last Index two cases not passed

#include
#define ll long long int
using namespace std;
ll findLastIndex(ll a[],ll n,ll x,ll currentIndex)
{
if(currentIndex==n)
{
return -1;
}
int index = findLastIndex(a,n,x,currentIndex+1);
if(index==-1 && a[index]==x)
{
return currentIndex;
}
else{
return index;
}
}
int main() {
ll n,a[100000],x;
cin>>n;
for(ll i=0;i<n;i++){
cin>>a[i];
}
cin>>x;
cout<<findLastIndex(a,n,x,0);
return 0;
}

Hey @shivansh.sm1 you need to make this change in your code in line 11 and 13
if(index==-1 && a[currentIndex]==x)
{
return currentIndex;
}
and then your code works 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.