In the input format in first line they have provided 2 value but you have scanned 3 value. In first for loop you have populated from index 1 . array contains a gerabage value. Lastly check the output format there they asked you to print only one integer last occurrence of key .But you are printing for each index if that element present then that index else -1. So read the question and input output format carefully.
check the code
using namespace std;
int a[n] ;
//travarse from last index as last occurance required
if(a[i]==m) // m found so break it.
// converting to 1 base indexing