-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.cpp
More file actions
32 lines (31 loc) · 710 Bytes
/
36.cpp
File metadata and controls
32 lines (31 loc) · 710 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main(){
//program to find kth smallest element in row wise sorted matrix.
int m, n;
cin>>m>>n;
int arr[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++)cin>>arr[i][j];
}
int k;
cin>>k;
int sm = INT_MAX, bg = INT_MIN;
for(int i=0;i<m;i++){
sm = min(sm, arr[i][0]);
bg = max(bg, arr[i][n-1]);
}
int req = k;
while(sm<bg){
int mid = (sm + bg)/2;
int x = 0;
for(int i=0;i<m;i++){
x += upper_bound(arr[i], arr[i] + n, mid) - arr[i];
}
if(x<req)sm = mid + 1;
else bg = mid;
}
cout<<sm;
return 0;
}