-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrix-Median.cpp
39 lines (33 loc) · 1.09 KB
/
Matrix-Median.cpp
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
38
39
#include<bits/stdc++.h>
using namespace std;
int getMedian(vector<vector<int>> &matrix)
{
int n = matrix.size();
int m = matrix[0].size();
int low = matrix[0][0], high = matrix[0][m - 1];
// Finding the minimum and maximum element in the matrix
for(int i=0; i<n; i++) {
low = min(low, matrix[i][0]);
high = max(high, matrix[i][m - 1]);
}
// Calculating the median position
int median = (m * n + 1) / 2;
while(low < high) {
// Calculating the mid element
int mid = low + (high - low) / 2;
int count = 0;
// Calculating the number of elements less than or equal to mid
for(int i=0; i<n; i++) {
count += upper_bound(matrix[i].begin(), matrix[i].end(), mid) - matrix[i].begin();
}
// If the count is less than the median, then we need to increase the mid
if(count < median) {
low = mid + 1;
// If the count is greater than the median, then we need to decrease the mid
} else {
high = mid;
}
}
// Returning the median
return low;
}