Given a binary matrix of **R** rows and **C** columns. We are allowed flip to any size of sub matrix. Flipping means changing 1 to 0 and 0 to 1. The task is maximize the number of 1s in the matrix. Output the maximum number of 1s.

Examples:

Input : R = 3, C =3 mat[][] = { 0, 0, 1, 0, 0, 1, 1, 0, 1 } Output : 8 Flip 0 0 10 011 01 to get 1 1 1 1 1 1 0 1 1 Input : R = 2, C = 3 mat[][] = { 0, 0, 0, 0, 0, 0 } Output : 6

Create a matrix ones[][] of R rows and C columns, which precomputes the number of ones in the submatrix from (0, 0) to (i, j) by

// Common elements in ones[i-1][j] and // ones[i][j-1] are ones[i-1][j-1] ones[i][j] = ones[i-1][j] + ones[i][j-1] - ones[i-1][j-1] + (mat[i][j] == 1)

Since, we are allowed to flip sub matrix only once. We iterate over all possible submatrices of all possible sizes for each cell (i, j) to (i + k – 1, i + k – 1). We calculate the total number of ones after the digits are filliped in the chosen submatrix.

Total number of ones in the final matrix after flipping submatrix (i, j) to (i + k – 1) will be Ones in the whole matrix – Ones in the chosen submatrix + Zeroes in the chosen sub matrix. That comes out to be :-

ones[R][C] – cal(i, j, i + k, j + k – 1) + k*k – cal(i, j, i + k – 1, j + k – 1)

where cal(a, b, c, d) denotes the number of ones in square submatrix of length c – a.

Now cal(x1, y1, x2, y2) can be define by:

ones[x2][y2] – ones[x2][y1 – 1] – ones[x1 – 1][y2] + ones[x1 – 1][y1 – 1].

Below is the C++ implementation of this approach:

`// C++ program to find maximum number of ones after ` `// one flipping in Binary Matrix ` `#include <bits/stdc++.h> ` `#define R 3 ` `#define C 3 ` `using` `namespace` `std; ` ` ` `// Return number of ones in square submatrix of size ` `// k x k starting from (x, y) ` `int` `cal(` `int` `ones[R + 1][C + 1], ` `int` `x, ` `int` `y, ` `int` `k) ` `{ ` ` ` `return` `ones[x + k - 1][y + k - 1] - ones[x - 1][y + k - 1] ` ` ` `- ones[x + k - 1][y - 1] + ones[x - 1][y - 1]; ` `} ` ` ` `// Return maximum number of 1s after flipping a submatrix ` `int` `sol(` `int` `mat[R][C]) ` `{ ` ` ` `int` `ans = 0; ` ` ` ` ` `// Precomputing the number of 1s ` ` ` `int` `ones[R + 1][C + 1] = {0}; ` ` ` `for` `(` `int` `i = 1; i <= R; i++) ` ` ` `for` `(` `int` `j = 1; j <= C; j++) ` ` ` `ones[i][j] = ones[i - 1][j] + ones[i][j - 1] - ` ` ` `ones[i - 1][j - 1] + ` ` ` `(mat[i - 1][j - 1] == 1); ` ` ` ` ` `// Finding the maximum number of 1s after flipping ` ` ` `for` `(` `int` `k = 1; k <= min(R, C); k++) ` ` ` `for` `(` `int` `i = 1; i + k - 1 <= R; i++) ` ` ` `for` `(` `int` `j = 1; j + k - 1 <= C; j++) ` ` ` `ans = max(ans, (ones[R][C] + k * k - ` ` ` `2 * cal(ones, i, j, k))); ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `mat[R][C] = {{0, 0, 1}, ` ` ` `{ 0, 0, 1}, ` ` ` `{ 1, 0, 1 } ` ` ` `}; ` ` ` ` ` `cout << sol(mat) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

7

**Time Complexity:** O(R*C*min(R, C))

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 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.

## Recommended Posts:

- Maximum value in a matrix which contain intersecting concentric submatrix
- Maximize sum of N X N upper left sub-matrix from given 2N X 2N matrix
- Maximize matrix as per given condition
- Program to check if a matrix is Binary matrix or not
- Submatrix Sum Queries
- Minimum sum submatrix in a given 2D array
- Summed Area Table - Submatrix Summation
- Nearest 1 in a binary matrix
- Count all 0s which are blocked by 1s in binary matrix
- Counting sets of 1s and 0s in a binary matrix
- Unique cells in a binary matrix
- Find if there is a rectangle in binary matrix with corners as 1
- Total coverage of all zeros in a binary matrix
- Maximum decimal value path in a binary matrix
- Construct Ancestor Matrix from a Given Binary Tree