-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary matrix having maximum number of 1s.cpp
84 lines (77 loc) · 1.63 KB
/
Binary matrix having maximum number of 1s.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
//{ Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution
{
public:
vector<int> findMaxRow(vector<vector<int>> mat, int N)
{
int i = 0, j = N - 1;
int ones = 0, row = 0;
while (i < N and j >= 0)
{
if (mat[i][j] == 1)
{
row = i;
ones++;
j--;
}
else
i++;
}
return {row, ones};
}
};
/*
vector<int> findMaxRow(vector<vector<int>> mat, int N)
{
int rowNumber = 0;
int maxCount = 0;
for (int i = 0; i < N; i++)
{
int count = 0;
for (int j = 0; j < N; j++)
{
if (mat[i][j] == 1)
{
count += 1;
}
}
if (count > maxCount)
{
maxCount = count;
rowNumber = i;
}
}
vector<int> v;
v.push_back(rowNumber);
v.push_back(maxCount);
return v;
}
*/
//{ Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<vector<int>> arr(n, vector<int>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> arr[i][j];
Solution obj;
vector<int> ans = obj.findMaxRow(arr, n);
for (int val : ans)
{
cout << val << " ";
}
cout << endl;
}
}
// } Driver Code Ends