Skip to content

Commit

Permalink
Create 9.Kth element in Matrix.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
mridul8920 authored Oct 16, 2021
1 parent d7ceeb4 commit e1b605b
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Matrix Solutions/9.Kth element in Matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
int kthSmallest(int mat[MAX][MAX], int n, int k)
{
//Your code here
int min=mat[0][0],max=mat[n-1][n-1];
while(min<max){
int mid=(min+max)/2,midpos=0;
for(int i=0;i<n;i++)
midpos+=upper_bound(mat[i],mat[i]+n,mid)-mat[i];
if(midpos<k)
min=mid+1;
else
max=mid;
}
return min;
}

0 comments on commit e1b605b

Please sign in to comment.