# Find row number of a binary matrix having maximum number of 1s

Given a binary matrix (containing only 0 and 1) of order n*n. All rows are sorted already, We need to find the row number with maximum number of 1s. Also find number of 1 in that row.

Note: in case of tie, print smaller row number.

**Examples :**

Input : mat[3][3] = {0, 0, 1, 0, 1, 1, 0, 0, 0} Output : Row number = 2, MaxCount = 2 Input : mat[3][3] = {1, 1, 1, 1, 1, 1, 0, 0, 0} Output : Row number = 1, MaxCount = 3

** Basic Approach : ** Traverse whole of matrix and for each row find the number of 1 and among all that keep updating the row number with maximum number of 1. This approach will result in O(n^2) time complexity.

**Better Approach : ** We can perform a better if we try to apply binary search for finding position of first 1 in each row and as per that we can find the number of 1 from each row as each row is in sorted order. This will result in O(nlogn) time complexity.

**Efficient Approach :** Start with top left corner with index (1, n) and try to go left until you reach last 1 in that row (jth column), now if we traverse left to that row, we will find 0, so switch to the row just below, with same column. Now your position will be (2, j) again in 2nd row if jth element if 1 try to go left until you find last 1 otherwise in 2nd row if jth element is 0 go to next row. So Finally say if you are at any ith row and jth column which is index of last 1 from right in that row, increment i. So now if we have Aij = 0 again increment i otherwise keep decreasing j until you find last 1 in that particular row.

Sample Illustration :

**Algorithm :**

for (int i=0, j=n-1; i<n;i++) { // find left most position of 1 in a row // find 1st zero in a row while (arr[i][j]==1) { row = i; j--; } } cout << "Row number =" << row+1; cout << "MaxCount =" << n-j;

## C++

`// CPP program to find row with maximum 1 ` `// in row sorted binary matrix ` `#include<bits/stdc++.h> ` `#define N 4 ` `using` `namespace` `std; ` ` ` `// function for finding row with maximum 1 ` `void` `findMax (` `int` `arr[][N]) ` `{ ` ` ` `int` `row = 0, i, j; ` ` ` `for` `(i=0, j=N-1; i<N;i++) ` ` ` `{ ` ` ` `// find left most position of 1 in a row ` ` ` `// find 1st zero in a row ` ` ` `while` `(arr[i][j] == 1 && j >= 0) ` ` ` `{ ` ` ` `row = i; ` ` ` `j--; ` ` ` `} ` ` ` `} ` ` ` `cout << ` `"Row number = "` `<< row+1; ` ` ` `cout << ` `", MaxCount = "` `<< N-1-j; ` `} ` ` ` `// driver program ` `int` `main() ` `{ ` ` ` `int` `arr[N][N] = {0, 0, 0, 1, ` ` ` `0, 0, 0, 1, ` ` ` `0, 0, 0, 0, ` ` ` `0, 1, 1, 1}; ` ` ` `findMax(arr); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find row with maximum 1 ` `// in row sorted binary matrix ` `class` `GFG { ` ` ` ` ` `static` `final` `int` `N = ` `4` `; ` ` ` ` ` `// function for finding row with maximum 1 ` ` ` `static` `void` `findMax(` `int` `arr[][]) { ` ` ` ` ` `int` `row = ` `0` `, i, j; ` ` ` ` ` `for` `(i = ` `0` `, j = N - ` `1` `; i < N; i++) { ` ` ` ` ` `// find left most position of 1 in ` ` ` `// a row find 1st zero in a row ` ` ` `while` `(j >= ` `0` `&& arr[i][j] == ` `1` `) { ` ` ` ` ` `row = i; ` ` ` `j--; ` ` ` `} ` ` ` `} ` ` ` ` ` `System.out.print(` `"Row number = "` ` ` `+ (row + ` `1` `)); ` ` ` `System.out.print(` `", MaxCount = "` ` ` `+ (N - ` `1` `- j)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` `int` `arr[][] = {{` `0` `, ` `0` `, ` `0` `, ` `1` `}, ` ` ` `{` `0` `, ` `0` `, ` `0` `, ` `1` `}, ` ` ` `{` `0` `, ` `0` `, ` `0` `, ` `0` `}, ` ` ` `{` `0` `, ` `1` `, ` `1` `, ` `1` `}}; ` ` ` `findMax(arr); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# python program to find row with ` `# maximum 1 in row sorted binary ` `# matrix ` ` ` `N ` `=` `4` ` ` `# function for finding row with ` `# maximum 1 ` `def` `findMax (arr): ` ` ` `row ` `=` `0` ` ` `j ` `=` `N ` `-` `1` ` ` `for` `i ` `in` `range` `(` `0` `, N): ` ` ` `# find left most position ` ` ` `# of 1 in a row find 1st ` ` ` `# zero in a row ` ` ` `while` `(arr[i][j] ` `=` `=` `1` ` ` `and` `j >` `=` `0` `): ` ` ` `row ` `=` `i ` ` ` `j ` `-` `=` `1` ` ` ` ` `print` `(` `"Row number = "` `, row ` `+` `1` `, ` ` ` `", MaxCount = "` `, N ` `-` `1` `-` `j) ` ` ` `# driver program ` `arr ` `=` `[ [` `0` `, ` `0` `, ` `0` `, ` `1` `], ` ` ` `[` `0` `, ` `0` `, ` `0` `, ` `1` `], ` ` ` `[` `0` `, ` `0` `, ` `0` `, ` `0` `], ` ` ` `[` `0` `, ` `1` `, ` `1` `, ` `1` `] ] ` ` ` `findMax(arr) ` ` ` `# This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find row with maximum ` `// 1 in row sorted binary matrix ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `N = 4; ` ` ` ` ` `// function for finding row with maximum 1 ` ` ` `static` `void` `findMax(` `int` `[,]arr) ` ` ` `{ ` ` ` `int` `row = 0, i, j; ` ` ` ` ` `for` `(i = 0, j = N - 1; i < N; i++) { ` ` ` ` ` `// find left most position of 1 in ` ` ` `// a row find 1st zero in a row ` ` ` `while` `(arr[i,j] == 1 && j >= 0) ` ` ` `{ ` ` ` `row = i; ` ` ` `j--; ` ` ` `} ` ` ` `} ` ` ` ` ` `Console.Write(` `"Row number = "` `+ (row + 1)); ` ` ` `Console.Write(` `", MaxCount = "` `+ (N - 1 - j)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[,]arr = {{0, 0, 0, 1}, ` ` ` `{0, 0, 0, 1}, ` ` ` `{0, 0, 0, 0}, ` ` ` `{0, 1, 1, 1}}; ` ` ` `findMax(arr); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find ` `// row with maximum 1 ` `// in row sorted ` `// binary matrix ` `$N` `= 4; ` ` ` ` ` `// function for finding ` `// row with maximum 1 ` `function` `findMax (` `$arr` `) ` `{ ` ` ` ` ` `global` `$N` `; ` ` ` `$row` `= 0; ` `$i` `; ` ` ` `$j` `=` `$N` `- 1; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$N` `; ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// find left most position ` ` ` `// of 1 in a row find 1st ` ` ` `// zero in a row ` ` ` `while` `(` `$arr` `[` `$i` `][` `$j` `] == 1 && ` ` ` `$j` `>= 0) ` ` ` `{ ` ` ` `$row` `= ` `$i` `; ` ` ` `$j` `--; ` ` ` `} ` ` ` `} ` ` ` `echo` `"Row number = "` `, ` `$row` `+ 1; ` ` ` `echo` `", MaxCount = "` `, ` `$N` `- 1 - ` `$j` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$arr` `= ` `array` `(` `array` `(0, 0, 0, 1), ` ` ` `array` `(0, 0, 0, 1), ` ` ` `array` `(0, 0, 0, 0), ` ` ` `array` `(0, 1, 1, 1)); ` ` ` `findMax(` `$arr` `); ` ` ` `// This code is contributed by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

Output:

Row number = 4, MaxCount = 3

This article is contributed by **Shivam Pradhan (anuj_charm)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find alphabet in a Matrix which has maximum number of stars around it
- Find row with maximum and minimum number of zeroes in given Matrix
- Find maximum path length in a binary matrix
- Find pair of rows in a binary matrix that has maximum bit difference
- Maximum number of ones in a N*N matrix with given constraints
- Minimum number of operations required to set all elements of a binary matrix
- Find number of cavities in a matrix
- Find Number of Even cells in a Zero Matrix after Q queries
- Find the number of distinct islands in a 2D matrix
- Find number of transformation to make two Matrix Equal
- Find the row with maximum number of 1s
- Python map function to find row with maximum number of 1's
- Maximum size rectangle binary sub-matrix with all 1s
- Maximum decimal value path in a binary matrix
- Check whether row or column swaps produce maximum size binary sub-matrix with all 1s
- Find row with maximum sum in a Matrix
- Find a sub matrix with maximum XOR
- Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Find column with maximum sum in a Matrix