Javatpoint Logo
Javatpoint Logo

Find the row with the maximum number of 1s

We are given a 2D boolean array in which each row is sorted in ascending order. Our task is to find the row with the highest number of true boolean values, also known as 1's, and return the index of that row.

Example 1:

Input:

Output:

1

Example 2:

Input:

Output:

2

Approach: Row-wise Traversal

A row-wise traversal is a common approach in matrix-related problems, where the matrix is traversed one row at a time. In this approach, we iterate through each row of the matrix and perform some operations on each element before moving on to the next row. The technique is especially useful when the matrix has a large number of rows but a small number of columns, as it minimizes the number of column-wise operations needed.

Filename: MaxOnesRowFinder.java

Output:

Max row index: 2

Complexity Analysis: The time complexity of the findMaxRow method in the MaxOnesRowFinder class is O(m*n), where m is the number of rows and n is the number of columns in the input matrix. It is because the method iterates through each row and then through each element in that row to count the number of 1s. The countOnes method, which is called by findMaxRow, has a time complexity of O(n), where n is the number of elements in the input row.

The space complexity of the MaxOnesRowFinder class is O(1), because the algorithm only uses a fixed number of variables to keep track of the row with the most 1s and the number of 1s in that row. The input matrix is not modified and no additional data structures are created. Therefore, the space complexity does not depend on the size of the input matrix.

Approach: Binary Search

The Binary Search approach to finding the row with the maximum number of 1s in a binary matrix involves iterating over each row and counting the number of 1s in each row using binary search. The algorithm maintains two variables to keep track of the maximum count of 1s seen so far and the corresponding row index. It applies the binary search technique to efficiently count the number of 1s in a given row.

Algorithm

Step 1: Define a public class BinaryMatrixRowWithMax1s.

Step 2: Define a public method rowWithMax1s(int[][] mat) to find the row with the maximum number of 1s in the binary matrix. In this method should take a 2D array of integers as input.

Step 3: Initialize variables to store the row index with the maximum number of 1s and the maximum count of 1s.

Step 4: Iterate through each row of the matrix and count the number of 1s in the current row using binary search. Update the maximum count and the corresponding row index if the current count exceeds the maximum count.

Step 5: Return the row index with a maximum number of 1s.

Implementation:

Filename: BinaryMatrixRowWithMax1s.java

Output:

Row with maximum 1s: 2

Complexity Analysis:

The time complexity of the BinaryMatrixRowWithMax1s method is O(m*logn), where m is the number of rows and n is the number of columns in the input matrix. It is because the method iterates through each row and performs binary search on each row to count the number of 1s, which takes logn time in the worst case. The time complexity of the countOnes method is also O(logn), as it performs binary search on a single row to count the number of 1s.

The space complexity of the code is O(1), as the only additional space used is for storing the maximum row index and maximum count of 1s, which are both integers. The input matrix is not modified during the execution of the code.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA