Why wont my code work for index n-1

question
https://hack.codingblocks.com/contests/c/512/369
my code
https://ide.codingblocks.com/s/36406

your order of if checks is wrong put them like this

if(a[i]==key){
j=i;
}
if(i==n-1){
cout<<j;
return ;
}