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?