- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find size of the largest ‘+’ formed by all ones in a binary matrix in C++
In this problem, we are given an NxN binary matrix bin[][]. Our task is to find the size of the largest ‘+’ formed by all ones in a binary matrix.
Let’s take an example to understand the problem,
Input
0 1 1 1 1 1 0 1 0
Output
5
Solution Approach
A simple solution to the problem is to find the largest ‘+’ for which we need to find the maximum number of 1’s in one direction for a point in the matrix which needs to be the same in all four directions for a given 1. For this, we will create one matrix for each side of the point i.e. 4. Each will store the number of consecutive 1’s for the given element. For all index values, we will find the maximum value which is the minimum of all consecutive ones in all four directions.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; #define N 7 int findLargestPlusSize(int mat[N][N]) { int conOneLeft[N][N], conOneRight[N][N], conOneTop[N][N], conOneBottom[N][N]; for (int i = 0; i < N; i++) { conOneTop[0][i] = mat[0][i]; conOneBottom[N - 1][i] = mat[N - 1][i]; conOneLeft[i][0] = mat[i][0]; conOneRight[i][N - 1] = mat[i][N - 1]; } for (int i = 0; i < N; i++) { for (int j = 1; j < N; j++) { if (mat[i][j] == 1) conOneLeft[i][j] = conOneLeft[i][j - 1] + 1; else conOneLeft[i][j] = 0; if (mat[j][i] == 1) conOneTop[j][i] = conOneTop[j - 1][i] + 1; else conOneTop[j][i] = 0; j = N - 1 - j; if (mat[j][i] == 1) conOneBottom[j][i] = conOneBottom[j + 1][i] + 1; else conOneBottom[j][i] = 0; if (mat[i][j] == 1) conOneRight[i][j] = conOneRight[i][j + 1] + 1; else conOneRight[i][j] = 0; j = N - 1 - j; } } int maxConOne = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++){ int ConOnes = min(min(conOneTop[i][j], conOneBottom[i][j]), min(conOneLeft[i][j], conOneRight[i][j])); if(ConOnes > maxConOne) maxConOne = ConOnes; } } if (maxConOne) return (4 * (maxConOne - 1) + 1); return 0; } int main() { int mat[N][N] = { { 1, 0, 1, 1, 1, 1, 0 }, { 1, 0, 1, 0, 1, 1, 1 }, { 1, 1, 1, 0, 1, 1, 0 }, { 0, 0, 0, 0, 1, 0, 0 }, { 1, 0, 1, 1, 1, 1, 1 }, { 1, 1, 1, 0, 1, 1, 1 }, { 1, 0, 0, 0, 1, 0, 0 }, }; cout<<"The size of the largest plus formed by ones is "<<findLargestPlusSize(mat); return 0; }
Output
The size of the largest plus formed by ones is 9
- Related Articles
- Maximum size rectangle binary sub-matrix with all 1s in C++
- Find perimeter of shapes formed with 1s in binary matrix in C++
- Maximum size rectangle binary sub-matrix with all 1s in C++ Program
- C++ Program to Find Size of the Largest Independent Set(LIS) in a Given a Binary Tree
- Program to find sum of all numbers formed by path of a binary tree in python
- Find the Largest Cube formed by Deleting minimum Digits from a number in C++
- Count all 0s which are blocked by 1s in binary matrix in C++
- Find length of the largest region in Boolean Matrix in C++
- Largest Component Size by Common Factor in C++
- Find the largest Complete Subtree in a given Binary Tree in C++
- Find duplicate rows in a binary matrix in C++
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix in C++
- Check If a String Contains All Binary Codes of Size K in C++
- Find the Largest Cube formed by Deleting minimum Digits from a number in Python
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix in C++ Program

Advertisements