Minimum area such that all submatrix of the size have same maximum value
Last Updated :
22 Jul, 2022
Given an N*M matrix Mat[][] containing all distinct integers, the task is to find the minimum area of the matrix (r*c, where 1 ≤ r ≤ N and 1 ≤ c ≤ M ) for every submatrix of size r*c the maximum value remains the same.
Examples:
Input: N = 4, M = 4, Mat[][] = {{9, 12, 6, 11}, {3, 15, 19, 4}, {1, 13, 8, 11}, {14, 7, 9, 15}}
Output: 3, 3
Explanation: The minimum size of the submatrix is 3*3.
All submatrix below that size does not contain the maximum 19.
Input: N = 1, M = 1, Mat[][] = {{25}}
Output: 1, 1
Approach: To solve the problem follow the below idea:
There can only be one maximum element in the matrix as all the elements are unique.
So all the submatrices must contain that element.
Say the position of the maximum is (i, j).
- If the row size of the matrix is less than (i+1), there can be a submatrix starting at 0th row which will not contain the maximum.
- Also if it has less than (N – i) rows, the submatrix starting from last row will not contain the maximum.
- So the row size will be the maximum between (i + 1) and (N – i).
- Similarly, the column size will be the maximum between (j + 1) and (M – j).
Follow the steps mentioned below to implement the idea:
- Run a nested loop and traverse the whole matrix to find the maximum element of the matrix.
- Store the location of the maximum element.
- Use the above observation to find the size of the submatrix.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
pair< int , int > findMinArea( int Mat[][MAX],
int N, int M)
{
int r, c;
int Max = INT_MIN;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
if (Mat[i][j] > Max) {
Max = Mat[i][j];
r = i;
c = j;
}
}
}
return make_pair(max(r + 1, N - r),
max(c + 1, M - c));
}
int main()
{
int N = 4, M = 4;
int Mat[][MAX] = { { 9, 12, 6, 11 },
{ 3, 15, 19, 4 },
{ 1, 13, 8, 11 },
{ 14, 7, 9, 15 } };
pair< int , int > X = findMinArea(Mat, N, M);
cout << X.first << " " << X.second << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int MAX = 1000 ;
public static int [] findMinArea( int Mat[][], int N,
int M)
{
int r = 0 , c = 0 ;
int Max = Integer.MIN_VALUE;
for ( int i = 0 ; i < N; i++) {
for ( int j = 0 ; j < M; j++) {
if (Mat[i][j] > Max) {
Max = Mat[i][j];
r = i;
c = j;
}
}
}
int ans[] = new int [ 2 ];
ans[ 0 ] = Math.max(r + 1 , N - r);
ans[ 1 ] = Math.max(c + 1 , M - c);
return ans;
}
public static void main(String[] args)
{
int N = 4 , M = 4 ;
int Mat[][] = { { 9 , 12 , 6 , 11 },
{ 3 , 15 , 19 , 4 },
{ 1 , 13 , 8 , 11 },
{ 14 , 7 , 9 , 15 } };
int X[] = findMinArea(Mat, N, M);
System.out.println(X[ 0 ] + " " + X[ 1 ]);
}
}
|
Python3
import sys
MAX = 1000 ;
def findMinArea(Mat, N, M):
r = 0 ; c = 0 ;
Max = - sys.maxsize - 1 ;
for i in range ( 0 ,N):
for j in range ( 0 ,M):
if (Mat[i][j] > Max ):
Max = Mat[i][j];
r = i;
c = j;
ans = [ 0 for i in range ( 2 )];
ans[ 0 ] = max (r + 1 , N - r);
ans[ 1 ] = max (c + 1 , M - c);
return ans;
if __name__ = = '__main__' :
N = 4 ; M = 4 ;
Mat = [[ 9 , 12 , 6 , 11 ],
[ 3 , 15 , 19 , 4 ],
[ 1 , 13 , 8 , 11 ],
[ 14 , 7 , 9 , 15 ]];
X = findMinArea(Mat, N, M);
print (X[ 0 ] , " " , X[ 1 ]);
|
C#
using System;
public class GFG {
public static int [] findMinArea( int [, ] Mat, int N,
int M)
{
int r = 0, c = 0;
int Max = Int32.MinValue;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
if (Mat[i, j] > Max) {
Max = Mat[i, j];
r = i;
c = j;
}
}
}
int [] ans = new int [2];
ans[0] = Math.Max(r + 1, N - r);
ans[1] = Math.Max(c + 1, M - c);
return ans;
}
static public void Main()
{
int N = 4, M = 4;
int [, ] Mat = new int [, ] { { 9, 12, 6, 11 },
{ 3, 15, 19, 4 },
{ 1, 13, 8, 11 },
{ 14, 7, 9, 15 } };
int [] X = findMinArea(Mat, N, M);
Console.WriteLine(X[0] + " " + X[1]);
}
}
|
Javascript
<script>
const MAX = 1000;
function findMinArea(Mat, N, M)
{
let r = 0, c = 0;
let Max = Number.MIN_VALUE;
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
if (Mat[i][j] > Max) {
Max = Mat[i][j];
r = i;
c = j;
}
}
}
let ans = new Array(2);
ans[0] = Math.max(r + 1, N - r);
ans[1] = Math.max(c + 1, M - c);
return ans;
}
let N = 4, M = 4;
let Mat = [ [ 9, 12, 6, 11 ],
[ 3, 15, 19, 4 ],
[ 1, 13, 8, 11 ],
[ 14, 7, 9, 15 ] ];
let X = findMinArea(Mat, N, M);
document.write(X[0] + " " + X[1]);
</script>
|
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...