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:
- Maximum size square sub-matrix with all 1s
- Print a given matrix in spiral form
- Search in a row wise and column wise sorted matrix
- A Boolean Matrix Question
- Matrix Chain Multiplication | DP-8
- Find the row with maximum number of 1s
- Print unique rows in a given boolean matrix
- Inplace (Fixed space) M x N size matrix transpose | Updated
- Maximum sum rectangle in a 2D matrix | DP-27
- Zigzag (or diagonal) traversal of Matrix
- Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
- Print all possible paths from top left to bottom right of a mXn matrix
- Count all possible paths from top left to bottom right of a mXn matrix
- Printing brackets in Matrix Chain Multiplication Problem
- Create a matrix with alternating rectangles of O and X