Given a boolean 2D array, where each row is sorted. Write a program to find the row with the maximum number of 1s.
Input Format:
The first line of the input consists of an integer, r that corresponds to the number of rows in the matrix.
The next line of the input consists of an integer, c that corresponds to the number of columns in the matrix.
The next 'm*n' lines in the input corresponds to the elements in the matrix.
Sample Input:
4
4
1 0 0 0
0 0 1 1
1 1 1 0
1 0 0 0
Sample Output:
2
Program Code:
#include<stdio.h>
int main()
{
int r,c;
scanf("%d%d",&r,&c);
int a[r][c];
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
int big=-1,s;
for(i=0;i<r;i++)
{
int count=0;
for(j=0;j<c;j++)
{
if(a[i][j]==1)
{
count++;
}
if(big<count)
{
s=i;
big=count;
}
}
}
printf("%d",s);
return 0;
}
Post A Comment:
0 comments: