C Exercises: Find the row with maximum number of 1s
C Array: Exercise-60 with Solution
Write a program in C to find the row with the maximum number of 1s.
Pictorial Presentation:
Sample Solution:
C Code:
#include <stdio.h>
#define R 5
#define C 5
int getFirstOccur(int arr1[], int l, int h)
{
if(h >= l)
{
int mid = l + (h - l)/2;
if ( ( mid == 0 || arr1[mid-1] == 0) && arr1[mid] == 1)
return mid;
else if (arr1[mid] == 0)
return getFirstOccur(arr1, (mid + 1), h);
else
return getFirstOccur(arr1, l, (mid -1));
}
return -1;
}
int findRowMaxOne(int arr2d[R][C])
{
int max_row_index = 0, max = -1;
int i, index;
for (i = 0; i < R; i++)
{
index = getFirstOccur (arr2d[i], 0, C-1);
if (index != -1 && C-index > max)
{
max = C - index;
max_row_index = i;
}
}
return max_row_index;
}
int main()
{
int arr2d[R][C] = { {0, 1, 0, 1,1},
{1, 1, 1, 1, 1},
{1, 0, 0, 1, 0},
{0, 0, 0, 0, 0},
{1, 0, 0, 0, 1}
};
int i,j;
//------------- print original 2D array ------------------
printf("The given 2D array is : \n");
for(i = 0; i < R; i++)
{
for(j=0; j<C ; j++)
{
printf("%d ", arr2d[i][j]);
}
printf("\n");
}
//------------------------------------------------------
printf("The index of row with maximum 1s is: %d " , findRowMaxOne(arr2d));
return 0;
}
Sample Output:
The given 2D array is : 0 1 0 1 1 1 1 1 1 1 1 0 0 1 0 0 0 0 0 0 1 0 0 0 1 The index of row with maximum 1s is: 1
Flowchart:

C Programming Code Editor:
Improve this sample solution and post your code through Disqus.
Previous: Write a program in C to return the counting sort on an array.
Next: Write a program in C to find maximum product subarray in a given array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
C Programming: Tips of the Day
C Programming - What is the argument for printf that formats a long?
Put an l (lowercased letter L) directly before the specifier.
unsigned long n; long m; printf("%lu %ld", n, m);
Ref : https://bit.ly/3dIwfkP
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises