-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmedian in row-wise sorted matrix.cpp
131 lines (90 loc) Β· 2.68 KB
/
median in row-wise sorted matrix.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
//////////////////////////////////////////////////////
//Question/Info
Median in a row-wise sorted Matrix
Medium Accuracy: 53.5% Submissions: 13951 Points: 4
Given a row wise sorted matrix of size RxC where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation:
Sorting matrix elements gives us
{1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1<= R,C <=150
1<= matrix[i][j] <=2000
author: srj_v
//////////////////////////////////////////////////////
*/
#include <bits/stdc++.h>
using namespace std;
#define _IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long double ld;
typedef long long int lli;
#pragma GCC optimize("Ofast")
void c_p_c()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
//////////////////////////////////////////////////////
int32_t main() {
///////////
// c_p_c();
///////////
_IOS
//////////
// code
/*
int t ; cin >> t; while(t--){}
*/
int median(vector<vector<int>> &matrix, int r, int c) {
// code here
int med = (r * c + 1) / 2;
// to get a desired median position... total/2 should be the position...
int low = 0;
int high = 5000;
int ans = 0 ;
/* for an element to be the median,
say n+1 where (n = even..) elements then our median
element must be in the middle and it must have n/2 elements
behind... so logic is we keep compare the mid element
index which is (low+high)/2 which would keep on changing
with the count of the elements that are lower that it ,
since it is a sorted matrix, we can use
upper bound on each and every row and take the count...
since sorted so we can take the bounds and subtract to get the
count... then just compare with the 'med'...*/
while (low < high) {
int count = 0 ;
int mid = (low + high) / 2;
for (int i = 0 ; i < r ; i++) {
count += upper_bound(matrix[i].begin(), matrix[i].end(), mid) - matrix[i].begin();
// to find the upper_bound(start iterator,end iterator, element...)
}
// if(count> med) high = mid;
// else low = mid+1;
if (count < med) low = mid + 1;
else high = mid;
}
return low;
}
// cerr << "time: " << clock() << " ms" << '\n';
return 0;
}
//////////////////////////////////////////////////////