What is error in it
Hi @kailash_01
See in your code you are getting last occurrence as 0 for each test case because in the last_occurance function in the for loop when a[mid]==m you are again declaring int ans which is why it becomes a local variable and after the for loop you just return -1 and ans that you declared outside the for loop has not changes. So just remove int from line 36. Also it is given in the question that n<10^5 so make array a as a[100000] this will then dont give run time error.
Here is your corrected code :
If your doubt is clear then mark it as resolved.