Maximum sub-matrix area having count of 1's one more than count of 0's
Last Updated :
11 Jul, 2025
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++
// 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;
}
Java
// Java implementation to find
// the maximum area sub-matrix
// having count of 1's
// one more than count of 0's
import java.util.*;
class GFG
{
static int start, finish;
// Function to find the length of longest
// subarray having count of 1's one more
// than count of 0's
static int lenOfLongSubarr(int []arr, int n)
{
// unordered_map 'um' implemented as
// hash table
HashMap<Integer,Integer> um = new HashMap<Integer, Integer>();
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.containsKey(sum))
um.put(sum,i);
// Check if 'sum-1' is present in 'um'
// or not
if (um.containsKey(sum - 1))
{
// Update 'start', 'finish'
// and maxLength
if (maxLen < (i - um.get(sum - 1)))
start = um.get(sum - 1) + 1;
finish = i;
maxLen = i - um.get(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
static void largestSubmatrix(int [][]mat, int n)
{
// Variables to store final
// and intermediate results
int finalLeft = 0, finalRight = 0,
finalTop = 0, finalBottom = 0;
int maxArea = 0, len;
finish = 0;
start=0;
int []temp = new int[n];
// Set the left column
for(int left = 0; left < n; left++)
{
// Initialize all elements of temp as 0
Arrays.fill(temp, 0);
// 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);
// 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
System.out.print("(Top, Left): (" + finalTop +
", " + finalLeft + ")\n");
System.out.print("(Bottom, Right): (" + finalBottom +
", " + finalRight + ")\n");
System.out.print("Maximum area: " + maxArea);
}
// Driver code
public static void main(String[] args)
{
int [][]mat = new int[][]{ { 1, 0, 0, 1 },
{ 0, 1, 1, 1 },
{ 1, 0, 0, 0 },
{ 0, 1, 0, 1 } };
int n = 4;
largestSubmatrix(mat, n);
}
}
// This code is contributed by pratham76
Python3
# Python implementation to find
# the maximum area sub-matrix
# having count of 1's
# one more than count of 0's
# function to find the length of longest
# subarray having count of 1's one more
# than count of 0's
def lenOfLongSubarr(arr, n, start, finish):
# unordered_map 'um' implemented as
# hash table
um = {}
sum = 0
maxLen = 0
# traverse the given array
for i in range(n):
# 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'
elif (sum not in um):
um[sum] = i
# check if 'sum-1' is present in 'um'
# or not
if (sum - 1 in um):
# 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,start,finish]
# function to find the maximum
# area sub-matrix having
# count of 1's one more than count of 0's
def largestSubmatrix(mat, n):
# variables to store final
# and intermediate results
temp = []
maxArea = 0
# set the left column
for left in range(n):
# Initialize all elements of temp as 0
temp = [0 for i in range(n)]
# Set the right column for the
# left column set by outer loop
for right in range(left, n):
# Calculate sum between current left and right
# for every row 'i', consider '0' as '-1'
for i in range(n):
if mat[i][right] == 0:
temp[i] -= 1
else:
temp[i] += 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
start = 0
finish = 0
fc = lenOfLongSubarr(temp, n, start, finish)
len = fc[0]
start = fc[1]
finish = fc[2]
# Compare with maximum area
# so far and accordingly update the
# final variables
if ((len != 0) and (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
print("(Top, Left): (",finalTop,", ",finalLeft,")")
print("(Bottom, Right): (",finalBottom, ", ",finalRight,")")
print("Maximum area: ", maxArea)
# Driver Code
mat = [[1, 0, 0, 1 ], [ 0, 1, 1, 1 ], [ 1, 0, 0, 0 ], [ 0, 1, 0, 1 ]]
n = 4
largestSubmatrix(mat, n)
# This code is contributed by rohitsingh07052
C#
// C# implementation to find
// the maximum area sub-matrix
// having count of 1's
// one more than count of 0's
using System;
using System.Collections.Generic;
using System.Collections;
class GFG{
// Function to find the length of longest
// subarray having count of 1's one more
// than count of 0's
static int lenOfLongSubarr(int []arr, int n,
ref int start, ref int finish)
{
// unordered_map 'um' implemented as
// hash table
Dictionary<int,
int> um = new Dictionary<int,
int>();
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.ContainsKey(sum))
um[sum] = i;
// Check if 'sum-1' is present in 'um'
// or not
if (um.ContainsKey(sum - 1))
{
// 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
static void largestSubmatrix(int [,]mat, int n)
{
// Variables to store final
// and intermediate results
int finalLeft = 0, finalRight = 0,
finalTop = 0, finalBottom = 0;
int maxArea = 0, len, start = 0, finish = 0;
int []temp = new int[n];
// Set the left column
for(int left = 0; left < n; left++)
{
// Initialize all elements of temp as 0
Array.Fill(temp, 0);
// 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, ref start,
ref 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
Console.Write("(Top, Left): (" + finalTop +
", " + finalLeft + ")\n");
Console.Write("(Bottom, Right): (" + finalBottom +
", " + finalRight + ")\n");
Console.Write("Maximum area: " + maxArea);
}
// Driver code
public static void Main(string[] args)
{
int [,]mat = new int[,]{ { 1, 0, 0, 1 },
{ 0, 1, 1, 1 },
{ 1, 0, 0, 0 },
{ 0, 1, 0, 1 } };
int n = 4;
largestSubmatrix(mat, n);
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// JavaScript implementation to find
// the maximum area sub-matrix
// having count of 1's
// one more than count of 0's
// function to find the length of longest
// subarray having count of 1's one more
// than count of 0's
function lenOfLongSubarr(arr, n, start, finish){
// unordered_map 'um' implemented as
// hash table
let um = new Map()
let sum = 0
let maxLen = 0
// traverse the given array
for(let 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.has(sum) == false)
um.set(sum,i)
// check if 'sum-1' is present in 'um'
// or not
if (um.has(sum - 1)){
// update 'start', 'finish'
// and maxLength
if (maxLen < (i - um.get(sum - 1))){
start = um.get(sum - 1) + 1
finish = i
maxLen = i - um.get(sum - 1)
}
}
}
// required maximum length
return [maxLen,start,finish]
}
// function to find the maximum
// area sub-matrix having
// count of 1's one more than count of 0's
function largestSubmatrix(mat, n){
// variables to store final
// and intermediate results
let temp = []
let maxArea = 0
let finalLeft,finalRight,finalTop,finalBottom;
// set the left column
for(let left=0;left<n;left++){
// Initialize all elements of temp as 0
temp = new Array(n).fill(0)
// Set the right column for the
// left column set by outer loop
for(let right = left;right<n;right++){
// Calculate sum between current left and right
// for every row 'i', consider '0' as '-1'
for(let i=0;i<n;i++){
if(mat[i][right] == 0)
temp[i] -= 1
else
temp[i] += 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
let start = 0
let finish = 0
let fc = lenOfLongSubarr(temp, n, start, finish)
let len = fc[0]
start = fc[1]
finish = fc[2]
// 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
document.write("(Top, Left): (" + finalTop + ", " + finalLeft + ")","</br>")
document.write("(Bottom, Right): (" + finalBottom + ", " + finalRight + ")","</br>")
document.write("Maximum area: "+ maxArea,"</br>")
}
// Driver Code
let mat = [[1, 0, 0, 1 ], [ 0, 1, 1, 1 ], [ 1, 0, 0, 0 ], [ 0, 1, 0, 1 ]]
let n = 4
largestSubmatrix(mat, n)
// This code is contributed by shinjanpatra
</script>
Output(Top, Left): (1, 1)
(Bottom, Right): (3, 3)
Maximum area: 9
Complexity Analysis:
- Time Complexity: O(N3).
- Auxiliary Space: O(N).
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem