Find last index- Recursion challenge

Can you please explain the use of this statement properly like a dry run as i’m not able to get when the recursive calls are happening exactly.
"int rr= lastIndex( arr, n, key, idx+1); "

#include

using namespace std;
int lastIndex( int arr[], int n,int key, int idx)
{
//base case
if(idx == n)
return -1;
int rr= lastIndex( arr, n, key, idx+1);

//rec case
if(key== arr[idx] && rr == (-1))
{
    return idx;
}
  return rr;

}
int main()
{
int n;
cin>>n;
int a[1000];
for( int i=0; i<n; i++)
cin>>a[i];
int key;
cin>>key;
cout<<lastIndex(a, n, key, 0);
return 0;
}

@samk86676, the trick to understand recursion is assuming the function will do its work all we need to do is utilize the result , if it does’nt make any sense then let me explain, so here , lastIndex(arr,n,ket,idx) function will give you the last index of key if present in the range idx to n-1 so what we are doing is for every index we are calling in the range[index+1,n) if the last index is present in this range then we will simply return the value returned by the function as even if arr[index] is same as key it won’t be the last Index as there is already a key on the right, if the value returned is -1 this means the key is not present in the range[index+1,n) so now we will check if arr[index] is key or not if yes then we will return the index if no then we will return -1 ,

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.