Algorithm Find minimum sum of k numbers from n arraysqueues
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you have n sorted arrays or queues and want the minimum possible sum of exactly k chosen numbers, the core task is to repeatedly take the smallest currently available value. When each input is already sorted in ascending order, a min-heap lets you do that efficiently without merging every element into one big list first.
This is a classic heap problem because you only ever need to compare the front candidates from each source.
Problem Setup
Assume each array or queue is sorted from smallest to largest. You may pick values from any source, and you want the smallest possible total after choosing exactly k values.
For example, if the inputs are:
- '
[1, 4, 9]' - '
[2, 3, 10]' - '
[5, 6]'
and k = 4, then the best choice is 1 + 2 + 3 + 4 = 10.
The reason sorting matters is that once you remove the current smallest value from one source, the next candidate from that source becomes the only new value worth considering immediately.
Heap-Based Strategy
The algorithm is:
- push the first element of every non-empty array into a min-heap
- pop the smallest element from the heap
- add it to the running sum
- push the next element from the same array, if one exists
- repeat until
kelements have been taken
This works because the heap always contains the smallest not-yet-used candidate from each source.
Python Example
Here is a runnable implementation:
The heap stores three pieces of information: the value, which array it came from, and the index inside that array.
Why This Is Better Than Full Merge
A brute-force approach would concatenate all arrays, sort the combined list, and sum the first k elements. That works, but it does more work than necessary when the inputs are already sorted.
The heap approach avoids touching every element eagerly. If the total number of arrays is n and you only need k values, the time complexity is O(k log n) after the initial heap setup. That is often much better than merging and sorting everything.
What If The Inputs Are Not Sorted
If the arrays or queues are not sorted, the heap approach no longer works directly because the front element is not guaranteed to be the smallest remaining choice from that source.
In that case you must either sort each source first or use a different selection strategy. The heap solution depends on the sorted-input guarantee.
Common Pitfalls
The biggest mistake is applying this algorithm to unsorted arrays. If the front elements are not the smallest remaining elements in their sources, the greedy heap choice is no longer valid.
Another pitfall is forgetting to handle the case where k is larger than the total number of available elements. The algorithm should either raise an error or define a clear fallback behavior.
A third issue is using a max-heap or storing the wrong tuple order in the heap. The smallest value must be the first comparison key so the heap pops the correct element.
Summary
- When each input source is sorted, a min-heap is the right tool for selecting the global next-smallest value.
- Push the first element of each array, then keep advancing only the source that supplied the last minimum.
- This yields the minimum possible sum of
kselected values. - The heap solution runs in
O(k log n)time after initialization. - The sorted-input assumption is essential for correctness.

