
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum sub-matrix area having count of 1's one more than count of 0’s in C++ program
In this problem, we are given a 2-D matrix of size nXn consisting of binary numbers (0/1). Our task is to create a program to find the Maximum submatrix area having count of 1’s one more than count of 0’s.
Let’s take an example to understand the problem,
Input
bin[N][N] = { {0, 1, 0, 0}, {1, 1, 0, 0}, {1, 0, 1, 1}, {0, 1, 0, 1} }
Output
9
Explanation
submatrix : bin[1][0], bin[1][1], bin[1][2] bin[2][0], bin[2][1], bin[2][2] bin[3][0], bin[3][1], bin[3][2] is the largest subarray with more 1’s one more than 0’s. Number of 0’s = 4 Number of 1’s = 5
Solution Approach
A simple approach is to find all submatrices possible from the matrix and then return the maximum area out of all.
This approach is easy to think and implement but takes a lot of time and requires nesting of loops that makes in time complexity of the order O(n^4). So, let’s discuss one more method which is more effective.
The idea here is to fix columns at the left and right of the matrix and then find the largest subarray which has the number of 0’s one more than the number of 1’s. We will calculate the sum at each row and then cumulate it. To find the maximum area having a count of 1’s one more than the number of 0’s.
Example
Program to illustrate the working of our solution,
#include <bits/stdc++.h> using namespace std; #define SIZE 10 int lenOfLongSubarr(int row[], int n, int& startInd, int& finishInd){ unordered_map<int, int> subArr; int sumVal = 0, maxSubArrLen = 0; for (int i = 0; i < n; i++) { sumVal += row[i]; if (sumVal == 1) { startInd = 0; finishInd = i; maxSubArrLen = i + 1; } else if (subArr.find(sumVal) == subArr.end()) subArr[sumVal] = i; if (subArr.find(sumVal − 1) != subArr.end()) { int currLen = (i − subArr[sumVal − 1]); if (maxSubArrLen < currLen) startInd = subArr[sumVal − 1] + 1; finishInd = i; maxSubArrLen = currLen; } } return maxSubArrLen; } int largestSubmatrix(int bin[SIZE][SIZE], int n){ int rows[n], maxSubMatArea = 0, currArea, longLen, startInd, finishInd; for (int left = 0; left < n; left++) { memset(rows, 0, sizeof(rows)); for (int right = left; right < n; right++) { for (int i = 0; i < n; ++i){ if(bin[i][right] == 0) rows[i] −= 1; else rows[i] += 1; } longLen = lenOfLongSubarr(rows, n, startInd, finishInd); currArea = (finishInd − startInd + 1) * (right − left + 1); if ((longLen != 0) && (maxSubMatArea < currArea)) { maxSubMatArea = currArea; } } } return maxSubMatArea; } int main(){ int bin[SIZE][SIZE] = { { 1, 0, 0, 1 }, { 0, 1, 1, 1 }, { 1, 0, 0, 0 }, { 0, 1, 0, 1 } }; int n = 4; cout<<"The maximum sub−matrix area having count of 1’s one more than count of 0’s is "<<largestSubmatrix(bin, n); return 0; }
Output
The maximum sub-matrix area having count of 1’s one more than count of 0’s is 9
- Related Articles
- Maximum sub-matrix area having count of 1’s one more than count of 0’s in C++
- Count number of binary strings of length N having only 0’s and 1’s in C++
- Count subarrays with equal number of 1’s and 0’s in C++
- Maximum length of segments of 0’s and 1’s in C++
- Count the number of 1’s and 0’s in a binary array using STL in C++
- Count subarrays consisting of only 0’s and only 1’s in a binary array in C++
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- C++ Largest Subtree having Equal No of 1's and 0's
- Count of divisors having more set bits than quotient on dividing N in C++
- Count of subsequences having maximum distinct elements in C++
- C# Program to Count the Number of 1's in the Entered Numbers
- Count all sub-sequences having product
- Maximum 0’s between two immediate 1’s in binary representation in C++
- Count number of binary strings without consecutive 1's in C
