Math, asked by superswordsman3081, 8 months ago

find sqare sub matrix of size n from the given sqare matrix of size m whose sum of element is maximum

Answers

Answered by Anonymous
0

Step-by-step explanation:

Print maximum sum square sub-matrix of given size. Given an N x N matrix, find a k x k submatrix where k <= N and k >= 1, such that sum of all the elements in submatrix is maximum. The input matrix can contain zero, positive and negative numbers.

Answered by Anonymous
5

Answer:

Given an N x N matrix, find a k x k submatrix where k <= N and k >= 1, such that sum of all the elements in submatrix is maximum. The input matrix can contain zero, positive and negative numbers.

For example consider below matrix, if k = 3, then output should print the sub-matrix enclosed in blue.

We strongly recommend you to minimize your browser and try this yourself first.

A Simple Solution is to consider all possible sub-squares of size k x k in our input matrix and find the one which has maximum sum. Time complexity of above solution is O(N2k2).

We can solve this problem in O(N2) time. This problem is mainly an extension of this problem of printing all sums. The idea is to preprocess the given square matrix. In the preprocessing step, calculate sum of all vertical strips of size k x 1 in a temporary square matrix stripSum[][]. Once we have sum of all vertical strips, we can calculate sum of first sub-square in a row as sum of first k strips in that row, and for remaining sub-squares, we can calculate sum in O(1) time by removing the leftmost strip of previous subsquare and adding the rightmost strip of new square.

Below is the implementation of above idea.

Step-by-step explanation:

// Driver program to test above function

int main()

{

int mat[N][N] = {{1, 1, 1, 1, 1},

{2, 2, 2, 2, 2},

{3, 8, 6, 7, 3},

{4, 4, 4, 4, 4},

{5, 5, 5, 5, 5},

};

int k = 3;

cout << "Maximum sum 3 x 3 matrix is\n";

printMaxSumSub(mat, k);

return 0;

Similar questions
Social Sciences, 11 months ago