# Kth smallest element in a row-wise and column-wise sorted 2D array | Set 1

Given an n x n matrix, where every row and column is sorted in non-decreasing order. Find the kth smallest element in the given 2D array.

For example, consider the following 2D array.

```        10, 20, 30, 40
15, 25, 35, 45
24, 29, 37, 48
32, 33, 39, 50
The 3rd smallest element is 20 and 7th smallest element is 30```

We strongly recommend to minimize the browser and try this yourself first.

The idea is to use min heap. Following are detailed step.
1) Build a min heap of elements from first row. A heap entry also stores row number and column number.
2) Do following k times.
…a) Get minimum element (or root) from min heap.
…b) Find row number and column number of the minimum element.
…c) Replace root with the next element from same column and min-heapify the root.
3) Return the last extracted root.

Following is C++ implementation of above algorithm.

```// kth largest element in a 2d array sorted row-wise and column-wise
#include<iostream>
#include<climits>
using namespace std;

// A structure to store an entry of heap.  The entry contains
// a value from 2D array, row and column numbers of the value
struct HeapNode {
int val;  // value to be stored
int r;    // Row number of value in 2D array
int c;    // Column number of value in 2D array
};

// A utility function to swap two HeapNode items.
void swap(HeapNode *x, HeapNode *y) {
HeapNode z = *x;
*x = *y;
*y = z;
}

// A utility function to minheapify the node harr[i] of a heap
// stored in harr[]
void minHeapify(HeapNode harr[], int i, int heap_size)
{
int l = i*2 + 1;
int r = i*2 + 2;
int smallest = i;
if (l < heap_size && harr[l].val < harr[i].val)
smallest = l;
if (r < heap_size && harr[r].val < harr[smallest].val)
smallest = r;
if (smallest != i)
{
swap(&harr[i], &harr[smallest]);
minHeapify(harr, smallest, heap_size);
}
}

// A utility function to convert harr[] to a max heap
void buildHeap(HeapNode harr[], int n)
{
int i = (n - 1)/2;
while (i >= 0)
{
minHeapify(harr, i, n);
i--;
}
}

// This function returns kth smallest element in a 2D array mat[][]
int kthSmallest(int mat[4][4], int n, int k)
{
// k must be greater than 0 and smaller than n*n
if (k <= 0 || k > n*n)
return INT_MAX;

// Create a min heap of elements from first row of 2D array
HeapNode harr[n];
for (int i = 0; i < n; i++)
harr[i] =  {mat[0][i], 0, i};
buildHeap(harr, n);

HeapNode hr;
for (int i = 0; i < k; i++)
{
// Get current heap root
hr = harr[0];

// Get next value from column of root's value. If the
// value stored at root was last value in its column,
int nextval = (hr.r < (n-1))? mat[hr.r + 1][hr.c]: INT_MAX;

// Update heap root with next value
harr[0] =  {nextval, (hr.r) + 1, hr.c};

// Heapify root
minHeapify(harr, 0, n);
}

// Return the value at last extracted root
return hr.val;
}

// driver program to test above function
int main()
{
int mat[4][4] = { {10, 20, 30, 40},
{15, 25, 35, 45},
{25, 29, 37, 48},
{32, 33, 39, 50},
};
cout << "7th smallest element is " << kthSmallest(mat, 4, 7);
return 0;
}
```

Output:

`7th smallest element is 30`

Time Complexity: The above solution involves following steps.
1) Build a min heap which takes O(n) time
2) Heapify k times which takes O(kLogn) time.
Therefore, overall time complexity is O(n + kLogn) time.

The above code can be optimized to build a heap of size k when k is smaller than n. In that case, the kth smallest element must be in first k rows and k columns.

We will soon be publishing more efficient algorithms for finding the kth smallest element.

# Company Wise Coding Practice    Topic Wise Coding Practice

• Deepak Maurya

#include

#include

void main()

{

int i,j,c=0,k,arr[100][100],m,n,min;

printf(“Enter Siz of 2d Array mXn :n”);

scanf(“%d %d”,&m,&n);

printf(“Enter Values of 2D MAtrix %dx%dn”,m,n);

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

scanf("%d",&arr[i][j]);

}

}

printf("nMatix U Entered :n");

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

printf(" %d",arr[i][j]);

}

printf("n");

}

printf("For Kth Smallest Enter K=");

scanf("%d",&k);

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

if((i+j)==k-1)

{

if(arr[i][j]<min)

{

min=arr[i][j];

c++;

}

if(c==k)

break;

}

}

}

printf("n%dth Smallest No. in Matrix is :-%d",k,min);

}

• Pulkit Agarwal

There are two ways I can think of.

1. it’s like sorting k sorted list. So keep a head pointer to all the rows. Keep picking the smallest and hence print the Kth smallest one. O(n^2)

2. Since the column is already sorted as well, We are looking at a equilateral triangle with vertices at (0,0), (0,K) and (K,0) . Total elements K^2. Using approach 1 you can find the Kth smallest in O(K^2)

• ultimate_coder

C++ STL lovers..

I have made heap of structure using STL.

http://ideone.com/8O2arY

• The posted algorithm is good for small k, but when k approaches n^2 (say n^2/2), this algorithm has time complexity O(n^2*log(n)). We can do better in this case. There is a simple O(n) algorithm to determine if a target value x is in the matrix. See:

This algorithm can be used to divide the matrix into two pieces, one below x, and the other above x, and we know the size of each piece. We can apply this algorithm O(log(k)) times to find a value that splits the matrix exactly into an upper left portion with k values, and a lower right portion with n^2-k values. This will run in O(n*log(k)) which when k approaches n^2 becomes O(n*log(n^2)) = O(2*n*log(n)) = O(n*log(n)). This is quite a bit faster than the posted algorithm.

• Guest

To find Kth smallest num. As we know that the in NxN matrix, the last element i.e (N-1, N-1) index element is the Largest of all other elements, means it will be the N^2 smallest num. For example in 3×3 matrix the (2,2) position element is the largest or in other words 9th smallest. Hence to find the Kth smallest we just need to find the “Greatest Integer(Sqrt(K))”, let this number be M and go to the last element of MxM submatrix directly. Like to find 6th smallest in a 5×5 matrix. We need to reach at the
“Greatest Integer(Sqrt(6))” = 3. We can directly go to the last element of the 3×3 submatrix which gives us 9th smallest.
From here onwards all we need to do is check the neighboring three positions i.e the (M-2, M-2) , (M-2, M-1), (M-1, M-2) index values and maximum of these will be the 8th smallest number. Jump to that cell and repeat the comparison with next 3 neighbors till we reach our Kth smallest number. (6th smallest in my example).
This takes At max O(N) time and O(1) auxiliary space (NO NEED OF HEAP) 🙂

• Guest

No need to use the heap. It can be easily done in O(n) time.
To find kth smallest all you need to do is search in the K x K submatrix starting from (0,0) position.
Start searching from the (K-1, K-1) last element (which is the largest one of this submatrix i.e. the “K^2 th smallest element” for example in 3×3 submatrix the (2, 2) position element would be the 9th smallest.
All you need to do now is check the 3 neighbor values i.e at position
(K-1, K-2) , (K-2, K-2), (K-2, K-1) . The maximum of them would be the
(K^2 – 1)th smallest element, Go to that cell of the submatrix now and
Repeat this process untill you reach to your Kth smallest one.
This takes O(n) time in worst case and does’nt use any heap :-p

• joy

good solution, I thought of the same thing for a similar question,,
find a number in a row/column sorted matrix.

• Guest

if the size of matrix is n x n and we’re looking for some Kth smallest where K > n , then we need to consider the whole matrix 🙂 i.e position (n-1, n-1) as start point 🙂 then apply the same method 🙂

• Munira

write function called negatives sum that takes an array of integers and returns the sum of all negative elements of the array

• Debabrata

why we cant apply simple merge technique?

#define ROW 4
#define COL 4
void merge(int arr[ROW][COL],int *sa){
int tmpArr[ROW*COL];
int i_end = COL;
int j_end = COL;
int i,ith_row ;
int j ,k;
for(ith_row=0;ith_row<COL;ith_row++){
sa[ith_row] = arr[0][ith_row];
}
for(ith_row =1 ; ith_row<ROW;ith_row++){
i=0;j=0;
for(k=0;k<i_end+j_end;k++){
if((i=j_end)|| sa[i] < arr[ith_row][j])){
tmpArr[k] = sa[i++];
}else{
tmpArr[k] = arr[ith_row][j++];
}

}
for(k=0;k<i_end+j_end;k++){
sa[k] = tmpArr[k];
}
i_end += COL;

}

}
void print_1D_array(int *arr,size_t size){
for(size_t i=0;i<size-1;i++){
std::cout<< arr[i]<<"-";
}
}
int main(){

int arr[ROW][COL] ={ {1,6,9,15},
{12,14,20,30},
{3,6,9,10},
{40,44,46,50}};

int sorted_array[ROW * COL];
merge(arr,sorted_array);
print_1D_array(sorted_array, ROW * COL);
return 0;
}

Using PriorityQueue Library in Java

import java.util.*;

class pair
{
X _data ;
Y _row;
Z _column;

pair(X x , Y y , Z z){_data=x; _row=y; _column=z;}

X data(){return _data;}
Y row(){return _row;}
Z column(){return _column;}

void setData(X x){_data=x;}
void setRow(Y y){_row=y;}
void setColumn(Z z){_column=z;}
}

public class KthSmallestNumberInRow_ColumnWiseSortedMatrix {

public static void main(String[]args)
{
int a[][]={ {10, 20, 30, 40},{15, 25, 35, 45},{24, 29, 37, 48},{32, 33, 39, 50} };
getKthSmallestElement(a);

}

private static void getKthSmallestElement(int[][] a) {

PriorityQueue<pair> pq=new PriorityQueue<pair>(1,
new Comparator<pair>(){
public int compare(pair t, pair t1) {
return t.data()-t1.data();
}
}
);

for(int i=0;i<4;i++) // number of column
{
pq.offer(new pair(a[0][i],0,i));
}

int count=4; // 4th smallest
for(int c=1;c<=count;c++)
{
pair res = pq.poll();
int row=res.row();
int col=res.column();
if(row+1<4)pq.offer(new pair(a[row+1][col],row+1,col));

}

}
}

Using PriorityQueue Library in Java

import java.util.*;

class pair
{
X _data ;
Y _row;
Z _column;

pair(X x , Y y , Z z){_data=x; _row=y; _column=z;}

X data(){return _data;}
Y row(){return _row;}
Z column(){return _column;}

void setData(X x){_data=x;}
void setRow(Y y){_row=y;}
void setColumn(Z z){_column=z;}
}

public class KthSmallestNumberInRow_ColumnWiseSortedMatrix {

public static void main(String[]args)
{
int a[][]={ {10, 20, 30, 40},{15, 25, 35, 45},{24, 29, 37, 48},{32, 33, 39, 50} };
getKthSmallestElement(a);

}

private static void getKthSmallestElement(int[][] a) {

PriorityQueue<pair> pq=new PriorityQueue<pair>(1,
new Comparator<pair>(){
public int compare(pair t, pair t1) {
return t.data()-t1.data();
}
}
);

for(int i=0;i<4;i++) // number of column
{
pq.offer(new pair(a[0][i],0,i));
}

int count=4; // 4th smallest
for(int c=1;c<=count;c++)
{
pair res = pq.poll();
int row=res.row();
int col=res.column();
if(row+1<4)pq.offer(new pair(a[row+1][col],row+1,col));

}

}
}

• novice

Do we really need a heap ? Why not

row = 1
col = 1
count = 0

while (count <= k ) {
// checks to ensure row , col < n omitted for simplicity
if (a[row][col+1] n)
++row
}
else {
++row
if ( row > n)
++col
}

++count
}

return a[row][col]

o(k) complexity

• Mearaj

I guess you have a bug here, you are checking only right side and down , rather you should check all the corners. Here after 24 , you shd move to 25 not 29.

10, 20, 30, 40
|
15, 25, 35, 45
|
24,–> 29 –> 37, 48

32, 33, 39, 50

• novice

You are right. But my main point is that a heap might not be necessary.

• Maulish Soni

• sunil

But this is as good as sorting n sorted arrays i.e you are not making use of the fact that they are sorted column wise as well.

• sunil
• sunil

ignore it .. you are taking care of that as well .. good solution

• gg

can we do it by using k-way merge?

• pulkit mehra

We can, but the time complexity would be more I guess, as we would have to process all the rows( we would have to merge the row elements) instead of processing n+k elements in the worst case for the above given method.

k-way merge is done using a heap.

• Chenzhan Liu

Instead of adding items into heap row-wisely, I suppose the following strategy might be more efficient:
(1)pop out the top value and get its row and col
(2)add matrix[row+1][col] and matrix[row][col+1] into heap, if they exist and have not been in heap

• pulkit mehra

Can you explain as to how will it be more effective ?

• Ankul

+1 on above soln. We can start the heap with empty heap each time pushing these 2 elements into it after the min is popped. This removes the extra overhead of O(n) or O(k).

• in this method the maximum size of heap at any point of time can O(n-1 + n-1) & insertion and extraction complexity is O(log (size of heap)) and hence the worst case complexity is O(k*log(n));

• Ahmed Saleh

I would just converted the 2D Array to 1D, sort it, then get kth element.

• omar salem

that’s N^2 right there

• Shivam Goel

n^2 (logn)

• Ahmed Saleh

Converting 2D array to 1D is O(N^2), sort it is O(NLogN).