Find top k (or most frequent) numbers in a stream
Find top k (or most frequent) numbers in a stream
Given an array of n numbers. Your task is to read numbers from the array and keep at-most K numbers at the top (According to their decreasing frequency) every time a new number is read. We basically need to print top k numbers sorted by frequency when input stream has included k distinct elements, else need to print all distinct elements sorted by frequency.
Examples:
Input : arr[] = {5, 2, 1, 3, 2}
k = 4
Output : 5 2 5 1 2 5 1 2 3 5 2 1 3 5
Explanation:
- After reading 5, there is only one element 5 whose frequency is max till now.
so print 5.- After reading 2, we will have two elements 2 and 5 with the same frequency.
As 2, is smaller than 5 but their frequency is the same so we will print 2 5.- After reading 1, we will have 3 elements 1, 2 and 5 with the same frequency,
so print 1 2 5.- Similarly after reading 3, print 1 2 3 5
- After reading last element 2 since 2 has already occurred so we have now a
frequency of 2 as 2. So we keep 2 at the top and then rest of the element
with the same frequency in sorted order. So print, 2 1 3 5.Input : arr[] = {5, 2, 1, 3, 4}
k = 4
Output : 5 2 5 1 2 5 1 2 3 5 1 2 3 4
Explanation:
- After reading 5, there is only one element 5 whose frequency is max till now.
so print 5.- After reading 2, we will have two elements 2 and 5 with the same frequency.
As 2, is smaller than 5 but their frequency is the same so we will print 2 5.- After reading 1, we will have 3 elements 1, 2 and 5 with the same frequency,
so print 1 2 5.
Similarly after reading 3, print 1 2 3 5- After reading last element 4, All the elements have same frequency
So print, 1 2 3 4.
Approach: The idea is to store the top k elements with maximum frequency. To store them a vector or an array can be used. To keep the track of frequencies of elements create a HashMap to store element-frequency pair. Given a stream of numbers, when a new element appears in the stream update the frequency of that element in HashMap and put that element at the end of the list of K numbers (total k+1 elements) now compare adjacent elements of the list and swap if higher frequency element is stored next to it.
Algorithm:
- Create a Hashmap hm, and an array of k + 1 length.
- Traverse the input array from start to end.
- Insert the element at k+1 th position of the array, update the frequency of that element in HashMap.
- Now, traverse the temp array from start to end – 1
- For very element, compare the frequency and swap if higher frequency element is stored next to it, if the frequency is same then swap is the next element is greater.
- print the top k element in each traversal of original array.
Implementation:
Output:
5 2 5 1 2 5 1 2 3 5 2 1 3 5
Complexity Analysis:
- Time Complexity: O( n * k ).
In each traversal the temp array of size k is traversed, So the time Complexity is O( n * k ). - Space Complexity:O(n).
To store the elements in HashMap O(n) space is required.
Comments
Post a Comment