Suppose a matrix of size N X N which contain concentric square submatrices centered at (xi, yi), where xi is the row number of center of ith concentric square matrix and yi is the column number of center of ith concentric square matrix. Concentric square matrix is of the form:
0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 1 . . . . . 1 0 0 1 . b b b . 1 0 0 1 . b a b . 1 0 0 1 . b b b . 1 0 0 1 . . . . . 1 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
where a is the center, b is a – 1, and the value will decrease as row or column increase.
Since there are multiple such sub-matrix, there are cells which are parts of more than one such submatrix. Those cells will have the value equal to the sum of values of intersecting submatrix. Given the value of N, m, (xi, yi, ai), where 1 <= i <= m and ai is the value at the center of ith concentric sub-matrix. The task is to find the maximum value in the matrix containing submatrices.
Input : N = 10, m = 2 (x1, y1, a1) = (3, 3, 3) (x2, y2, a2) = (7, 7, 4) Output : 4 Maxtrix that will be form:
Input : N = 10, m = 1 (x1, y1, a1) = (4, 5, 6) Output : 6
The idea is to make a 2D matrix mat and find the value of each cell including the cell having the intersection of multiple concentric submatrix. Now, observe value of each cell can be find by max(0, a – max(p – xi, q – yi)) where a is the value of at the center of ith concentric sub-matrix, p is the row number of the cell, q is the column number of the cell and (xi, yi) is the center location of ith concentric sub-matrix center. So, after finding the matrix mat, we will traverse the matrix to find the maximum value in the matrix.
Below is C++ implementation of this approach:
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.
- Maximum sum of any submatrix of a Matrix which is sorted row-wise and column-wise
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's
- Print concentric rectangular pattern in a 2d matrix
- Largest possible square submatrix with maximum AND value
- Maximize the binary matrix by filpping submatrix once
- Check if matrix A can be converted to B by changing parity of corner elements of any submatrix
- Check if a matrix contains a square submatrix with 0 as boundary element
- Count of submatrix with sum X in a given Matrix
- Bitwise XOR of a submatrix of a matrix generated from a given array
- Smallest submatrix required to be removed such that sum of the remaining matrix is divisible by K
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Find Maximum Length Of A Square Submatrix Having Sum Of Elements At-Most K
- Submatrix of given size with maximum 1's
- Smallest submatrix with Kth maximum XOR
- Count straight lines intersecting at a given point
- Summed Area Table - Submatrix Summation
- Minimum sum submatrix in a given 2D array
- XOR of a submatrix queries
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Submatrix Sum Queries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.