1

There is an algorithm interview question:

We have n sorted arrays, how to find the m-th frequent element in the aggregated array of n arrays? Moreover, how to save space? Even compromise on some time complexity.

What I can think is that enumerate all the elements of n arrays and use a hashmap to record their frequency, then sort hashmap respect to the value (frequency). But then there is no difference with the one array case.

1
  • what is your attempted code Commented Nov 15, 2020 at 15:00

2 Answers 2

1

Walk over all arrays in parallel using n pointers

1 4 7 12 34
2 6 9 12 25

The walk would look like this

1  1  4  7  7  12 12 34
*  2  2  2  9  12 25 34

You do need a hash map in order to count the number of occurrences of elements in the cut. E.g. at the second step in the example, your cut contains 1 and 2.

Also, you need two min-heaps, one for every cut to be able to choose the array to advance along and another one to store m most repetitive elements.

The complexity would be expected O(#elements * (log(n) + log(m))). The space requirement is O(n + m).


But if you really need to save space you can consider all these n sorted arrays as one big unsorted, sort it with something like heapsort and choose the longest subarray of duplicates. This would require O(#elements * log(#elements)) time but only O(1) space.

Sign up to request clarification or add additional context in comments.

Comments

1

You do an n-way merge, but instead of writing out the merged array, you just count the length of each run of duplicate values and remember the longest m in a min-heap.

This takes O(total_length * (log n + log m)) time, and O(n) space.

It's a combination of common SO questions. Search above on "merge k sorted lists" and "kth largest"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.