-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path26.cpp
More file actions
50 lines (47 loc) · 1 KB
/
Copy path26.cpp
File metadata and controls
50 lines (47 loc) · 1 KB
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
//Find the row with maximum number of 1’s.
//https://practice.geeksforgeeks.org/problems/row-with-max-1s/0
int rowWithMax1s(vector<vector<int> > arr, int n, int m) {
// code here
int hash[n]={0};
bool flag=false;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(arr[i][j]==1){
hash[i]++;
flag=true;
}
}
}
int ind=0,maxi=hash[0];
for(int i=0;i<n;i++){
if(hash[i]>maxi){
maxi=hash[i];
ind=i;
}
}
if(!flag){
return -1;
}
return ind;
}
//Algo2 (better time complexity O(m+n)
int rowWithMax1s(vector<vector<int> > arr, int n, int m) {
// code here
int r = 0, c = m - 1, curcount = 0, max_count = 0, rno = 0;
while (r < n && c >= 0) {
if (arr[r][c] == 1) {
c--;
curcount++;
} else {
r++;
}
if (curcount > max_count) {
max_count = curcount;
rno = r;
}
}
if(curcount==0){
return -1;
}
return rno;
}