-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFind the row with maximum number of 1s
More file actions
61 lines (55 loc) · 1.38 KB
/
Copy pathFind the row with maximum number of 1s
File metadata and controls
61 lines (55 loc) · 1.38 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
50
51
52
53
54
55
56
57
58
59
60
61
// Online C++ compiler to run C++ program online
#include <iostream>
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
#define r 4
#define c 4
int first(bool arr[], int low, int high)
{
if(high >= low)
{
// Get the middle index
int mid = low + (high - low)/2;
// Check if the element at middle index is first 1
if ( ( mid == 0 || arr[mid-1] == 0) && arr[mid] == 1)
return mid;
// If the element is 0, recur for right side
else if (arr[mid] == 0)
return first(arr, (mid + 1), high);
// If element is not first 1, recur for left side
else
return first(arr, low, (mid -1));
}
return -1;
}
int max1(bool mat[r][c])
{
int i, index;
int max_row=0;
int max = first(mat[0],0,c-1);
//cout<<max;
for(i=1;i<r;i++)
{
if(max!=-1&&mat[i][c-max-1]==1){
index= first(mat[i],0,c-max);
if(index!=-1&&c-index>max){
max =c-index;
max_row=i;
}
}
else {
max=first(mat[i],0,c-1);
}
}
return max_row;
}
int main()
{
bool mat[r][c] = { {0, 0, 0, 1},
{0, 1, 1, 1},
{1, 1, 1, 1},
{0, 0, 0, 0}};
cout << "Index of row with maximum 1s is " << max1(mat);
return 0;
}