PROBLEM DESCRIPTION
Given a boolean 2D array, where each row is sorted. Find the row with the maximum number of 1s.
SOLUTION
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
class Sol
{
public static int maxOnes (int mat[][], int n, int m)
{
int max = 0;
int res = 0;
for(int i=0; i<n; i++){
int count = countOnes(mat[i], m);
if(count > max){
max = count;
res = i;
}
}
return res;
}
// get number of 1st using binary search
public static int countOnes(int[] arr, int n){
int l = 0;
int r = n-1;
int pos = -1;
while(l <= r){
int m = l + (r - l)/2;
if(arr[m] == 1){
pos = m;
r = m-1;
}else{
l = m+1;
}
}
if(pos == -1)
return 0;
return n - pos;
}
}