Skip to content

Conversation

@Sharvari1086
Copy link

To maintain the x-sum for every sliding window, we use two ordered sets: large (which always stores the top-x most frequent elements) and small (which stores the rest). Each entry in these sets is a pair (frequency, number). When we add a number, we update its frequency and move it to the correct set. When we remove a number, we reduce its frequency and again update its place. If large has fewer than x elements, we always fill it first. If a new pair is too small for large, it goes to small. If it belongs in large, we move the smallest element of large to small. When we delete a pair from large, we replace it by moving the largest element from small. We also track the running sum of “frequency × number” for all elements in large, so the x-sum is always available instantly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant