MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / median

Method median

MedianOfRowWiseSortedMatrix.java:4–28  ·  view source on GitHub ↗
(int matrix[][], int r, int c)

Source from the content-addressed store, hash-verified

2
3class Solution {
4 int median(int matrix[][], int r, int c) {
5 // code here
6
7 int startVal=0,endVal=2000,midVal;
8 int n=r*c;
9 while(startVal<=endVal)
10 {
11 midVal=(endVal+startVal)/2;
12 int ans=0;
13 for(int i=0;i<r;i++)
14 {
15 int l=0,h=c-1;
16 while(l<=h)
17 {
18 int m=l+(h-l)/2;
19 if(matrix[i][m]<=midVal) l=m+1;
20 else h=m-1;
21 }
22 ans+=l;
23 }
24 if(ans<=n/2) startVal=midVal+1;
25 else endVal=midVal-1;
26 }
27 return startVal;
28 }
29}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected