-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33.cpp
More file actions
30 lines (29 loc) · 695 Bytes
/
33.cpp
File metadata and controls
30 lines (29 loc) · 695 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
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main(){
//program to find median 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 mn = INT_MAX, mx = INT_MIN;
for(int i=0;i<m;i++){
mn = min(mn, arr[i][0]);
mx = max(mx, arr[i][n-1]);
}
int req = (m * n + 1) / 2;
while(mn<mx){
int mid = (mx + mn)/2;
int p = 0;
for(int i=0;i<m;i++){
p += upper_bound(arr[i], arr[i] + n, mid) - arr[i];
}
if(p<req) mn = mid + 1;
else mx = mid;
}
cout<<mn<<endl;
return 0;
}