Sunday, November 17, 2024
Google search engine
HomeData Modelling & AIMaximum value in a matrix which contain intersecting concentric submatrix

Maximum value in a matrix which contain intersecting concentric submatrix

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++




//C++ Program to find the maximum value in a matrix
//which contain intersecting concentric submatrix
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100
 
// Return the maximum value in intersecting
// concentric submatrix.
int maxValue(int n, int m, int x[], int y[], int a[])
{
    int c[MAXN][MAXN] = { 0 };
 
    // For each center of concentric sub-matrix.
    for (int i = 0; i < m; ++i) {
 
        // for each row
        for (int p = 0; p < n; ++p) {
 
            // for each column
            for (int q = 0; q < n; ++q) {
 
                // finding x distance.
                int dx = abs(p - x[i]);
 
                // finding y distance.
                int dy = abs(q - y[i]);
 
                // maximum of x distance and y distance
                int d = max(dx, dy);
 
                // assigning the value.
                c[p][q] += max(0, a[i] - d);
            }
        }
    }
 
    // Finding the maximum value in the formed matrix.
    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;
}
 
 
// Driven Program
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




// Java Program to find the
// maximum value in a matrix
// which contain intersecting
// concentric submatrix
import java.io.*;
 
class GFG
{
static int MAXN = 100;
 
// Return the maximum value
// in intersecting
// concentric submatrix.
static int maxValue(int n, int m,
                    int x[], int y[],
                    int a[])
{
    int c[][] = new int[MAXN][MAXN];
 
    // For each center of
    // concentric sub-matrix.
    for (int i = 0; i < m; ++i)
    {
 
        // for each row
        for (int p = 0; p < n; ++p)
        {
 
            // for each column
            for (int q = 0; q < n; ++q)
            {
 
                // finding x distance.
                int dx = Math.abs(p - x[i]);
 
                // finding y distance.
                int dy = Math.abs(q - y[i]);
 
                // maximum of x distance
                // and y distance
                int d = Math.max(dx, dy);
 
                // assigning the value.
                c[p][q] += Math.max(0, a[i] - d);
            }
        }
    }
 
    // Finding the maximum
    // value in the formed matrix.
    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;
}
 
 
// Driven Code
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));
}
}
 
// This code is contributed by anuj_67.


Python 3




# Python 3 Program to find the maximum
# value in a matrix which contain
# intersecting concentric submatrix
MAXN = 100
 
# Return the maximum value in intersecting
# concentric submatrix.
def maxValue( n, m, x, y, a):
 
    c = [[0 for x in range(MAXN)]
            for y in range(MAXN)]
 
    # For each center of concentric sub-matrix.
    for i in range( m):
 
        # for each row
        for p in range(n) :
 
            # for each column
            for q in range( n) :
 
                # finding x distance.
                dx = abs(p - x[i])
 
                # finding y distance.
                dy = abs(q - y[i])
 
                # maximum of x distance and y distance
                d = max(dx, dy)
 
                # assigning the value.
                c[p][q] += max(0, a[i] - d)
 
    # Finding the maximum value in
    # the formed matrix.
    res = 0
    for i in range(n) :
        for j in range(n) :
            res = max(res, c[i][j])
    return res
 
# Driver Code
if __name__ == "__main__":
     
    n = 10
    m = 2
    x = [ 3, 7 ]
    y = [ 3, 7 ]
    a = [ 4, 3 ]
 
    print(maxValue(n, m, x, y, a))
 
# This code is contributed by ita_c


C#




// C# Program to find the maximum
// value in a matrix which contain
// intersecting concentric submatrix
using System;
 
class GFG
{
static int MAXN = 100;
 
// Return the maximum value in intersecting
// concentric submatrix.
static int maxValue(int n, int m,
                    int[] x, int[] y,
                    int[] a)
{
    int[,] c = new int[MAXN, MAXN];
 
    // For each center of
    // concentric sub-matrix.
    for (int i = 0; i < m; ++i)
    {
 
        // for each row
        for (int p = 0; p < n; ++p)
        {
 
            // for each column
            for (int q = 0; q < n; ++q)
            {
 
                // finding x distance.
                int dx = Math.Abs(p - x[i]);
 
                // finding y distance.
                int dy = Math.Abs(q - y[i]);
 
                // maximum of x distance
                // and y distance
                int d = Math.Max(dx, dy);
 
                // assigning the value.
                c[p,q] += Math.Max(0, a[i] - d);
            }
        }
    }
 
    // Finding the maximum
    // value in the formed matrix.
    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;
}
 
// Driver Code
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>
 
// Javascript Program to find the
// maximum value in a matrix
// which contain intersecting
// concentric submatrix
var maxN = 100;
 
// Return the maximum value in intersecting
// concentric submatrix.
function maxValue(n, m, x, y, a)
{
    var c = Array.from(Array(maxN), () =>
    Array(maxN).fill(0));
 
    // For each center of concentric sub-matrix.
    for (var i = 0; i < m; ++i) {
 
        // for each row
        for (var p = 0; p < n; ++p) {
 
            // for each column
            for (var q = 0; q < n; ++q) {
 
                // finding x distance.
                var dx = Math.abs(p - x[i]);
 
                // finding y distance.
                var dy = Math.abs(q - y[i]);
 
                // maximum of x distance
                // and y distance
                var d = Math.max(dx, dy);
 
                // assigning the value.
                c[p][q] += Math.max(0, a[i] - d);
            }
        }
    }
 
    // Finding the Math.maximum value in
    // the formed matrix.
    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;
}
 
 
// Driven Program
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>


Output

4

Complexity Analysis:

  • Time complexity: O(n*n*m)
  • Auxiliary Space: O(MAX*MAX)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments