Given a **N x N** binary matrix. The problem is finding the maximum area sub-matrix having a count of 1’s one more than count of 0’s.

**Examples:**

Input: mat[][] = { {1, 0, 0, 1}, {0, 1, 1, 1}, {1, 0, 0, 0}, {0, 1, 0, 1} }Output: 9 The sub-matrix defined by the boundary values(1, 1)and(3, 3). { {1, 0, 0, 1}, {0,1, 1, 1}, {1,0, 0, 0}, {0,1, 0, 1} }

**Naive Approach:** Check every possible rectangle in given 2D matrix. This solution requires 4 nested loops and the time complexity of this solution would be O(n^4).

**Efficient Approach:** An efficient approach will be to use Longest subarray having count of 1s one more than count of 0s which reduces the time complexity to O(n^3). The idea is to fix the left and right columns one by one and find the maximum length contiguous rows having the count of 1’s one more than the count of 0’s for every left and right column pair. Find top and bottom row numbers (which have a maximum length) for every fixed left and right column pair. To find the top and bottom row numbers, calculate the sum of elements in every row from left to right and store these sums in an array say temp[] (consider 0 as -1 while adding it). So temp[i] indicates the sum of elements from left to right in row i. Using the approach in Longest subarray having count of 1s one more than count of 0s , temp[] array is used to get the maximum length subarray of temp[] having count of 1’s one more than a count of 0’s by obtaining the start and end row numbers, then these values can be used to find maximum possible area with left and right as boundary columns. To get the overall maximum area, compare this area with the maximum area so far.

Below is the implementation of the above approach:

`// C++ implementation to find ` `// the maximum area sub-matrix ` `// having count of 1's ` `// one more than count of 0's ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `#define SIZE 10 ` ` ` `// function to find the length of longest ` `// subarray having count of 1's one more ` `// than count of 0's ` `int` `lenOfLongSubarr(` `int` `arr[], ` `int` `n, ` ` ` `int` `& start, ` `int` `& finish) ` `{ ` ` ` `// unordered_map 'um' implemented as ` ` ` `// hash table ` ` ` `unordered_map<` `int` `, ` `int` `> um; ` ` ` `int` `sum = 0, maxLen = 0; ` ` ` ` ` `// traverse the given array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// accumulating sum ` ` ` `sum += arr[i]; ` ` ` ` ` `// when subarray starts form index '0' ` ` ` `if` `(sum == 1) { ` ` ` `start = 0; ` ` ` `finish = i; ` ` ` `maxLen = i + 1; ` ` ` `} ` ` ` ` ` `// make an entry for 'sum' if it is ` ` ` `// not present in 'um' ` ` ` `else` `if` `(um.find(sum) == um.end()) ` ` ` `um[sum] = i; ` ` ` ` ` `// check if 'sum-1' is present in 'um' ` ` ` `// or not ` ` ` `if` `(um.find(sum - 1) != um.end()) { ` ` ` ` ` `// update 'start', 'finish' ` ` ` `// and maxLength ` ` ` `if` `(maxLen < (i - um[sum - 1])) ` ` ` `start = um[sum - 1] + 1; ` ` ` `finish = i; ` ` ` `maxLen = i - um[sum - 1]; ` ` ` `} ` ` ` `} ` ` ` ` ` `// required maximum length ` ` ` `return` `maxLen; ` `} ` ` ` `// function to find the maximum ` `// area sub-matrix having ` `// count of 1's one more than count of 0's ` `void` `largestSubmatrix(` `int` `mat[SIZE][SIZE], ` `int` `n) ` `{ ` ` ` `// variables to store final ` ` ` `// and intermediate results ` ` ` `int` `finalLeft, finalRight, finalTop, finalBottom; ` ` ` `int` `temp[n], maxArea = 0, len, start, finish; ` ` ` ` ` `// set the left column ` ` ` `for` `(` `int` `left = 0; left < n; left++) { ` ` ` ` ` `// Initialize all elements of temp as 0 ` ` ` `memset` `(temp, 0, ` `sizeof` `(temp)); ` ` ` ` ` `// Set the right column for the ` ` ` `// left column set by outer loop ` ` ` `for` `(` `int` `right = left; right < n; right++) { ` ` ` ` ` `// Calculate sum between current left and right ` ` ` `// for every row 'i', consider '0' as '-1' ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `temp[i] += mat[i][right] == 0 ? -1 : 1; ` ` ` ` ` `// function to set the 'start' and 'finish' ` ` ` `// variables having index values of ` ` ` `// temp[] which contains the longest ` ` ` `// subarray of temp[] having count of 1's ` ` ` `// one more than count of 0's ` ` ` `len = lenOfLongSubarr(temp, n, start, finish); ` ` ` ` ` `// Compare with maximum area ` ` ` `// so far and accordingly update the ` ` ` `// final variables ` ` ` `if` `((len != 0) && (maxArea < (finish - start + 1) ` ` ` `* (right - left + 1))) { ` ` ` `finalLeft = left; ` ` ` `finalRight = right; ` ` ` `finalTop = start; ` ` ` `finalBottom = finish; ` ` ` `maxArea = (finish - start + 1) * (right - left + 1); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print final values ` ` ` `cout << ` `"(Top, Left): ("` `<< finalTop << ` `", "` ` ` `<< finalLeft << ` `")\n"` `; ` ` ` ` ` `cout << ` `"(Bottom, Right): ("` `<< finalBottom << ` `", "` ` ` `<< finalRight << ` `")\n"` `; ` ` ` ` ` `cout << ` `"Maximum area: "` `<< maxArea; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `mat[SIZE][SIZE] = { { 1, 0, 0, 1 }, ` ` ` `{ 0, 1, 1, 1 }, ` ` ` `{ 1, 0, 0, 0 }, ` ` ` `{ 0, 1, 0, 1 } }; ` ` ` `int` `n = 4; ` ` ` `largestSubmatrix(mat, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

(Top, Left): (1, 1) (Bottom, Right): (3, 3) Maximum area: 9

**Time Complexity:** O(N^{3}).

**Auxiliary Space:** O(N).

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.

## Recommended Posts:

- Count of submatrix with sum X in a given Matrix
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Summed Area Table - Submatrix Summation
- Submatrix of given size with maximum 1's
- Largest possible square submatrix with maximum AND value
- Maximum value in a matrix which contain intersecting concentric submatrix
- Find Maximum Length Of A Square Submatrix Having Sum Of Elements At-Most K
- Maximum count of pairs which generate the same sum
- Count of subarrays whose maximum element is greater than k
- Find the row whose product has maximum count of prime factors
- Maximum count of elements divisible on the left for any element
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count of binary strings of length N having equal count of 0's and 1's
- Maximum difference of count of black and white vertices in a path containing vertex V
- Maximum sum of any submatrix of a Matrix which is sorted row-wise and column-wise
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Submatrix Sum Queries
- XOR of a submatrix queries
- Minimum sum submatrix in a given 2D array

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.