Problem Link: http://www.spoj.com/problems/FREQUENT
The approach I am using to solve this problem is described here. This is an O((N+Q)\sqrt{N}) approach. But I am getting TLE.
In short, my approach was to use Mo’s Algorithm to sort the queries in the required order, but then I couldn’t figure out how to perform the following operations in optimal time:
- add a single element
- remove a single element
- find answer of a range
All I could think was O(log(n)) for the above operations. But then I read Fcdkbear’s comment and tried to implement it the way he explained it. But, I still get TLE.
My code: https://ideone.com/E9kNVs
Can anyone explain how to do this or whether I am doing something wrong in my approach?