ROW WITH MAXIMUM NUMBER OF 1s
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: 
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;
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: