Maximum value in a matrix which contain intersecting concentric submatrix
Last Updated :
19 Aug, 2022
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.
So, after
Examples:
Input : N = 10, m = 2
(x1, y1, a1) = (3, 3, 3)
(x2, y2, a2) = (7, 7, 4)
Output : 4
Matrix 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:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100
int maxValue( int n, int m, int x[], int y[], int a[])
{
int c[MAXN][MAXN] = { 0 };
for ( int i = 0; i < m; ++i) {
for ( int p = 0; p < n; ++p) {
for ( int q = 0; q < n; ++q) {
int dx = abs (p - x[i]);
int dy = abs (q - y[i]);
int d = max(dx, dy);
c[p][q] += max(0, a[i] - d);
}
}
}
int res = 0;
for ( int i = 0; i < n; ++i) {
for ( int j = 0; j < n; ++j) {
res = max(res, c[i][j]);
}
}
return res;
}
int main()
{
int n = 10;
int m = 2;
int x[] = { 3, 7 };
int y[] = { 3, 7 };
int a[] = { 4, 3 };
cout << maxValue(n, m, x, y, a) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int MAXN = 100 ;
static int maxValue( int n, int m,
int x[], int y[],
int a[])
{
int c[][] = new int [MAXN][MAXN];
for ( int i = 0 ; i < m; ++i)
{
for ( int p = 0 ; p < n; ++p)
{
for ( int q = 0 ; q < n; ++q)
{
int dx = Math.abs(p - x[i]);
int dy = Math.abs(q - y[i]);
int d = Math.max(dx, dy);
c[p][q] += Math.max( 0 , a[i] - d);
}
}
}
int res = 0 ;
for ( int i = 0 ; i < n; ++i)
{
for ( int j = 0 ; j < n; ++j)
{
res = Math.max(res, c[i][j]);
}
}
return res;
}
public static void main (String[] args)
{
int n = 10 ;
int m = 2 ;
int x[] = { 3 , 7 };
int y[] = { 3 , 7 };
int a[] = { 4 , 3 };
System.out.println(maxValue(n, m, x,
y, a));
}
}
|
Python 3
MAXN = 100
def maxValue( n, m, x, y, a):
c = [[ 0 for x in range (MAXN)]
for y in range (MAXN)]
for i in range ( m):
for p in range (n) :
for q in range ( n) :
dx = abs (p - x[i])
dy = abs (q - y[i])
d = max (dx, dy)
c[p][q] + = max ( 0 , a[i] - d)
res = 0
for i in range (n) :
for j in range (n) :
res = max (res, c[i][j])
return res
if __name__ = = "__main__" :
n = 10
m = 2
x = [ 3 , 7 ]
y = [ 3 , 7 ]
a = [ 4 , 3 ]
print (maxValue(n, m, x, y, a))
|
C#
using System;
class GFG
{
static int MAXN = 100;
static int maxValue( int n, int m,
int [] x, int [] y,
int [] a)
{
int [,] c = new int [MAXN, MAXN];
for ( int i = 0; i < m; ++i)
{
for ( int p = 0; p < n; ++p)
{
for ( int q = 0; q < n; ++q)
{
int dx = Math.Abs(p - x[i]);
int dy = Math.Abs(q - y[i]);
int d = Math.Max(dx, dy);
c[p,q] += Math.Max(0, a[i] - d);
}
}
}
int res = 0;
for ( int i = 0; i < n; ++i)
{
for ( int j = 0; j < n; ++j)
{
res = Math.Max(res, c[i, j]);
}
}
return res;
}
public static void Main ()
{
int n = 10;
int m = 2;
int [] x = { 3, 7 };
int [] y = { 3, 7 };
int [] a = { 4, 3 };
Console.Write(maxValue(n, m, x, y, a));
}
}
|
Javascript
<script>
var maxN = 100;
function maxValue(n, m, x, y, a)
{
var c = Array.from(Array(maxN), () =>
Array(maxN).fill(0));
for ( var i = 0; i < m; ++i) {
for ( var p = 0; p < n; ++p) {
for ( var q = 0; q < n; ++q) {
var dx = Math.abs(p - x[i]);
var dy = Math.abs(q - y[i]);
var d = Math.max(dx, dy);
c[p][q] += Math.max(0, a[i] - d);
}
}
}
var res = 0;
for ( var i = 0; i < n; ++i) {
for ( var j = 0; j < n; ++j) {
res = Math.max(res, c[i][j]);
}
}
return res;
}
var n = 10;
var m = 2;
var x = [ 3, 7 ];
var y = [ 3, 7 ];
var a = [ 4, 3 ];
document.write(maxValue(n, m, x, y, a));
</script>
|
Complexity Analysis:
- Time complexity: O(n*n*m)
- Auxiliary Space: O(MAX*MAX)
Share your thoughts in the comments
Please Login to comment...