I am solving problems on mo's algorithm and came across this problem FREQ2. I am using an array (inv_freq) where inv_freq[x] stores the no of distinct elements having frequency x along with a dynamically updated square root decomposed array of this to find the max frequency. I believe its complexity is O((Q+N)*sqrt(N)). My solution is timing out. Can someone help with a better way to find this maximum?
UPDATE: I saw this idea which uses an array where array[x] is the number of elements having frequency more than x which can be incremented and decremented with single addition of element or deletion of element to our range and binary searching over this array to find the maximum frequency.