Given a boolean 2D array, where each row is sorted. Find the row with the maximum number of 1s.
Input matrix 0 1 1 1 0 0 1 1 1 1 1 1 // this row has maximum 1s 0 0 0 0 Output: 2
A simple method is to do a row wise traversal of the matrix, count the number of 1s in each row and compare the count with max. Finally, return the index of row with maximum 1s. The time complexity of this method is O(m*n) where m is number of rows and n is number of columns in matrix.
We can do better. Since each row is sorted, we can use Binary Search to count of 1s in each row. We find the index of first instance of 1 in each row. The count of 1s will be equal to total number of columns minus the index of first 1.
See the following code for implementation of the above approach.
Index of row with maximum 1s is 2
Time Complexity: O(mLogn) where m is number of rows and n is number of columns in matrix.
The above solution can be optimized further. Instead of doing binary search in every row, we first check whether the row has more 1s than max so far. If the row has more 1s, then only count 1s in the row. Also, to count 1s in a row, we don’t do binary search in complete row, we do search in before the index of last max.
Following is an optimized version of the above solution.
The worst case time complexity of the above optimized version is also O(mLogn), the will solution work better on average. Thanks to Naveen Kumar Singh for suggesting the above solution.
The worst case of the above solution occurs for a matrix like following.
0 0 0 … 0 1
0 0 0 ..0 1 1
0 … 0 1 1 1
….0 1 1 1 1
Following method works in O(m+n) time complexity in worst case.
Step1: Get the index of first (or leftmost) 1 in the first row.
Step2: Do following for every row after the first row
…IF the element on left of previous leftmost 1 is 0, ignore this row.
…ELSE Move left until a 0 is found. Update the leftmost index to this index and max_row_index to be the current row.
The time complexity is O(m+n) because we can possibly go as far left as we came ahead in the first step.
Following is C++ implementation of this method.
Thanks to Tylor, Ankan and Palash for their inputs.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Print all possible paths from the first row to the last row in a 2D array
- Find row number of a binary matrix having maximum number of 1s
- Python map function to find row with maximum number of 1's
- Find row with maximum and minimum number of zeroes in given Matrix
- Find maximum element of each row in a matrix
- Find row with maximum sum in a Matrix
- Find maximum sum from top to bottom row with no adjacent diagonal elements
- Find the row whose product has maximum count of prime factors
- Find all matrix elements which are minimum in their row and maximum in their column
- Maximum weight path ending at any element of last row in a matrix
- Replace every matrix element with maximum of GCD of row or column
- Check whether row or column swaps produce maximum size binary sub-matrix with all 1s
- Maximum sum of elements from each row in the matrix
- Maximum sum of a Matrix where each value is from a unique row and column
- Maximum sum of any submatrix of a Matrix which is sorted row-wise and column-wise
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Find a common element in all rows of a given row-wise sorted matrix
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.