Time limit exceeded https://www.codechef.com/viewsolution/41927912

Solution: 41927912 | CodeChef

I need help regarding 1 past contest problem. DEFECTIVE ITEM
m getting Time limit exceeded error. So can any1 suggest me any solution to overcome it as I am new to codechef. I am more comfortable with python so have coded in it. If anything is wrong pls. do let me know.

Since it is given that all the items are unique, we can store the index of each item in a map (or a dictionary in python). Now, we compute the prefix sum array and the suffix sum array. To answer each query, we first find the index of that element, and then take the minimum of the prefix sum at that position and the suffix sum at that position which we have already computed. Link to my submission in C++.
https://www.codechef.com/viewsolution/41713297

okay, thanks for the solution!
But just out of curiosity is my logic/approach incorrect?

okay, thanks for the solution!
But just out of curiosity is my logic/approach incorrect?

Your approach is correct. However, it works in O(n*q) and thus it gives Time limit exceeded.

okay, Thanks a lot!