Question:
Maximum no of 1's row - Geeksforgeeks
Link : Geeksforgeeks
Try without storing into the actual array...
Input:
2 3 4 1 1 0 1 1 0 0 1 1 1 0 1 2 2 0 1 1 1
Output:
0
1
Explanation:
- Run two loops.
- Run the outer loop from 0 to no_of rows.
- Run the inner loop from 0 to no_of columns.
- For each inner loop , check if that element is equal to one . If yes, increase the counter by 1 and end of the loop confirm whether ir is max or not and make certain changes and make the counter to zero again.
Code:
#include<stdio.h>
int func()
{
int r,c;
scanf("%d",&r);
// no of rws
scanf("%d",&c);
//no of colums
int i,j;
int max=0;
//max counter
int count,a,row_index,max_row;
for(i=0;i<r;i++)
{
count=0;
for(j=0;j<c;j++)
{
scanf("%d",&a);
if(a==1)
{
count++;
}
}
if(count>max)
{
max=count;
row_index=i;
}
}
printf("%d\n",row_index);
}
int main()
{
int cases,i;
scanf("%d",&cases);
for(i=0;i<cases;i++)
{
func();
}
return 0;
}

No comments:
Post a Comment