# Find m-th smallest value in k sorted arrays

Given k sorted arrays of possibly different sizes, find m-th smallest value in the merged array.

Examples:

Input: m = 5 arr[][] = { {1, 3}, {2, 4, 6}, {0, 9, 10, 11}} ; Output: 4 Explanation The merged array would be {0 1 2 3 4 6 9 10 11}. The 5-th smallest element in this merged array is 4. Input: 6 = 2 arr[][] = { {1, 3, 20}, {2, 4, 6}} ; Output: 20

A **simple solution** is to create an output array and and one by one copy all arrays to it. Finally, sort the output array using. This approach takes O(N Logn N) time where N is count of all elements.

An **efficient solution** is to use heap data structure. The time complexity of heap based solution is O(m Log k).

1. Create a min heap of size k and insert 1st element in all the arrays into the heap

2. Repeat following steps m times

…..a) Remove minimum element from heap (minimum is always at root) and store it in output array.

…..b) Insert next element from the array from which the element is extracted. If the array doesn’t have any more elements, then do nothing.

3. Print the last removed item.

`// C++ program to find m-th smallest element ` `// in the merged arrays. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// A pair of pairs, first element is going to ` `// store value, second element index of array ` `// and third element index in the array. ` `typedef` `pair<` `int` `, pair<` `int` `, ` `int` `> > ppi; ` ` ` `// This function takes an array of arrays as an ` `// argument and all arrays are assumed to be ` `// sorted. It returns m-th smallest element in ` `// the array obtained after merging the given ` `// arrays. ` `int` `mThLargest(vector<vector<` `int` `> > arr, ` `int` `m) ` `{ ` ` ` `// Create a min heap with k heap nodes. Every ` ` ` `// heap node has first element of an array ` ` ` `priority_queue<ppi, vector<ppi>, greater<ppi> > pq; ` ` ` ` ` `for` `(` `int` `i = 0; i < arr.size(); i++) ` ` ` `pq.push({ arr[i][0], { i, 0 } }); ` ` ` ` ` `// Now one by one get the minimum element ` ` ` `// from min heap and replace it with next ` ` ` `// element of its array ` ` ` `int` `count = 0; ` ` ` `int` `i = 0, j = 0; ` ` ` `while` `(count < m && pq.empty() == ` `false` `) { ` ` ` `ppi curr = pq.top(); ` ` ` `pq.pop(); ` ` ` ` ` `// i ==> Array Number ` ` ` `// j ==> Index in the array number ` ` ` `i = curr.second.first; ` ` ` `j = curr.second.second; ` ` ` ` ` `// The next element belongs to same array as ` ` ` `// current. ` ` ` `if` `(j + 1 < arr[i].size()) ` ` ` `pq.push({ arr[i][j + 1], { i, j + 1 } }); ` ` ` ` ` `count++; ` ` ` `} ` ` ` ` ` `return` `arr[i][j]; ` `} ` ` ` `// Driver program to test above functions ` `int` `main() ` `{ ` ` ` `vector<vector<` `int` `> > arr{ { 2, 6, 12 }, ` ` ` `{ 1, 9 }, ` ` ` `{ 23, 34, 90, 2000 } }; ` ` ` ` ` `int` `m = 4; ` ` ` `cout << mThLargest(arr, m); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

9

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find the Kth smallest element in the sorted generated array
- Generate all possible sorted arrays from alternate elements of two given sorted arrays
- Find relative complement of two sorted arrays
- Find the closest pair from two sorted arrays
- Find common elements in three sorted arrays
- Find k pairs with smallest sums in two arrays
- Merge k sorted arrays | Set 2 (Different Sized Arrays)
- Minimize (max(A[i], B[j], C[k]) - min(A[i], B[j], C[k])) of three different sorted arrays
- Merge 3 Sorted Arrays
- Merge k sorted arrays | Set 1
- Merge two sorted arrays
- K-th Element of Two Sorted Arrays
- Find the smallest and second smallest elements in an array
- Median of two sorted arrays with different sizes in O(log(min(n, m)))
- Union and Intersection of two sorted arrays
- Median of two sorted arrays of same size
- Print uncommon elements from two sorted arrays
- Merge two sorted arrays with O(1) extra space
- Count pairs from two sorted arrays whose sum is equal to a given value x
- Merging two unsorted arrays in sorted order

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.