Given a binary matrix, the task is to find whether row swaps or column swaps give maximum size sub-matrix with all 1’s. In a row swap, we are allowed to swap any two rows. In a column swap we are allowed to swap any two columns. Output “Row Swap” or “Column Swap” and the maximum size.
Input : 1 1 1 1 0 1 Output : Column Swap 4 By swapping column 1 and column 2(0-based indexing), index (0, 0) to (1, 1) makes the largest binary sub-matrix. Input : 0 0 0 1 1 0 1 1 0 0 0 0 1 1 0 Output : Row Swap 6 Input : 1 1 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 1 1 0 Output : Row Swap 8
The idea is to find both row swap and column swap maximum size binary submatrix and compare.
To find the maximum sized binary sub-matrix with row swaps allowed, make a 2-D array, say dp[i][j]. Each value of dp[i][j] contains the number of consecutive 1s on right side of (i,j) in i-th row. Now, store each column in the 1-D temporary array one by one, say b and sort, and find maximum b[i] * (n – i), since b[i] is indicating the sub-matrix width and (n – i) is sub-matrix height.
Similarly, to find the maximum size binary sub-matrix with column swap allowed, find dp[i][j], where each value contains the number of consecutive 1 below the (i, j) in j-th column. Similarly, store each row in the 1-D temporary array one by one, say b and sort. Find maximum b[i] * (m – i), since b[i] is indicating the submatrix height and (n – i) is submatrix width.
Below is the implementation of this approach:
Row Swap 6
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
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 sum of any submatrix of a Matrix which is sorted row-wise and column-wise
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Submatrix of given size with maximum 1's
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Find if a binary matrix exists with given row and column sums
- Leftmost Column with atleast one 1 in a row-wise sorted binary matrix
- Leftmost Column with atleast one 1 in a row-wise sorted binary matrix | Set 2
- Replace every matrix element with maximum of GCD of row or column
- Maximum sum of a Matrix where each value is from a unique row and column
- Print all possible paths from the first row to the last row in a 2D array
- Check if sums of i-th row and i-th column are same in matrix
- Check if the given matrix is increasing row and column wise
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Print all elements in sorted order from row and column 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.
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Print an N x M matrix such that each row and column has all the vowels in it
- Maximize the binary matrix by filpping submatrix once