-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary matrix having maximum number of 1s.cpp
61 lines (53 loc) · 1.41 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
//{ 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) {
//code here
int max_1s=0;
int max_row=0;
for(int i=0;i<mat.size();i++){
int start=0,end=mat[0].size()-1;
int first_pos=-1;
while(start<=end){
int mid=start+(end-start)/2;
if(mat[i][mid]==1){
first_pos=mid;
end=mid-1;
}else{
start=mid+1;
}
}
// cout<<first_pos<<" "<<mat[0].size()-first_pos<<endl;
if(first_pos!=-1 && max_1s<mat[0].size()-first_pos){
max_1s=mat[0].size()-first_pos;
max_row=i;
}
}
return {max_row,max_1s};
}
};
//{ 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