-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathMatrix-Median
More file actions
37 lines (33 loc) · 757 Bytes
/
Matrix-Median
File metadata and controls
37 lines (33 loc) · 757 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
33
34
35
36
37
//problem Statement : https://www.interviewbit.com/problems/matrix-median/submissions/ //
int countSmallerNo(vector<int>&v,int mid){
int low=0;
int high=v.size()-1;
while(low<=high){
int mi=(low+high)>>1;
if(v[mi]<=mid)
low=mi+1;
else
high=mi-1;
}
return low;
}
int Solution::findMedian(vector<vector<int> > &A) {
int low=1;
int high=1e9;
int n=A.size();
int m=A[0].size();
while(low<=high){
int mid=(low+high)>>1;
int count=0;
for(int i=0;i<n;i++){
count+=countSmallerNo(A[i],mid);
}
if(count<=(n*m)/2){
low=mid+1;
}
else{
high=mid-1;
}
}
return low;
}