algoblazerEarly Access
LearnProblemsLeaderboard
Log inSign up

All problems

‹ Back to map
1 shown · 0/261 solved

K Smallest Across Sorted Arrays

SilverCommunity Beta
Asked ateBay
Solve problem →
2000ms256MBAdded Aug 3, 2026Heaps & Priority Queues

You are given arrays, a list of integer arrays where each array is individually sorted in non-decreasing order (some arrays may be empty), and an integer k.

Return the k smallest elements among all elements of all arrays, in non-decreasing order. Duplicate values count separately; each occurrence is its own element.

Examples

Example 1

Input: arrays = [[1,4,7],[2,5,8],[3,6,9]], k = 5
Output: [1,2,3,4,5]
Explanation: The five smallest elements across all three arrays are 1, 2, 3, 4, and 5.

Example 2

Input: arrays = [[],[1,3],[2]], k = 2
Output: [1,2]
Explanation: Empty arrays contribute nothing; the two smallest elements overall are 1 and 2.

Example 3

Input: arrays = [[1,1],[1,2]], k = 3
Output: [1,1,1]
Explanation: The value 1 occurs three times in total, and each occurrence counts.

Constraints

  • 1≤1 \leq1≤ arrays.length ≤100\leq 100≤100
  • 0≤0 \leq0≤ arrays[i].length ≤104\leq 10^4≤104
  • 1≤N≤1051 \leq N \leq 10^51≤N≤105, where NNN is the total number of elements across all arrays
  • −109≤-10^9 \leq−109≤ arrays[i][j] ≤109\leq 10^9≤109
  • Each arrays[i] is sorted in non-decreasing order.
  • 1≤1 \leq1≤ k ≤N\leq N≤N

Details

Solved by8 people
Time limit2000ms
Memory256MB
AddedAug 3, 20265 weeks ago
Heaps & Priority Queues

More like this