-
Notifications
You must be signed in to change notification settings - Fork 91
/
rowWithMax1.cpp
56 lines (47 loc) · 1.01 KB
/
rowWithMax1.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
Given a boolean 2D array of n x m dimensions where each row is sorted. Find the 0-based index of the first row that has the maximum number of 1's.
Example 1:
Input:
N = 4 , M = 4
Arr[][] = {{0, 1, 1, 1},
{0, 0, 1, 1},
{1, 1, 1, 1},
{0, 0, 0, 0}}
Output: 2
Explanation: Row 2 contains 4 1's (0-based
indexing).
Example 2:
Input:
N = 2, M = 2
Arr[][] = {{0, 0}, {1, 1}}
Output: 1
Explanation: Row 1 contains 2 1's (0-based
indexing).
class Solution{
public:
int rowWithMax1s(vector<vector<int> > arr, int n, int m) {
int count=0;
int ans=0;
int maxi = 0;
for(int i=0;i<n;i++)
{
count=0;
for(int j=0;j<m;j++)
{
if(arr[i][j] == 1)
{
count++;
}
}
if(maxi < count)
{
maxi = count;
ans = i;
}
}
if(maxi == 0)
{
return -1;
}
return ans;
}
};