Skip to content
Related Articles

Related Articles

Minimum area such that all submatrix of the size have same maximum value

View Discussion
Improve Article
Save Article
  • 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++




// C++ code to implement the above approach
 
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
 
// Find M and N for which in every N*M
// area the max element remains same
pair<int, int> findMinArea(int Mat[][MAX],
                           int N, int M)
{
    // Initialize the variable
    int r, c;
    int Max = INT_MIN;
 
    // Find position of maximum element
    // from the grid
    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 minimum value of  submatrix
    return make_pair(max(r + 1, N - r),
                     max(c + 1, M - c));
}
 
// Driver Code
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 } };
 
    // Function call
    pair<int, int> X = findMinArea(Mat, N, M);
    cout << X.first << " " << X.second << endl;
    return 0;
}

Java




// Java code to implement the above approach
import java.io.*;
 
class GFG {
    static int MAX = 1000;
    // Find M and N for which in every N*M
    // area the max element remains same
    public static int[] findMinArea(int Mat[][], int N,
                                    int M)
    {
        // Initialize the variable
        int r = 0, c = 0;
        int Max = Integer.MIN_VALUE;
 
        // Find position of maximum element
        // from the grid
        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];
        // Return minimum value of  submatrix
        ans[0] = Math.max(r + 1, N - r);
        ans[1] = Math.max(c + 1, M - c);
        return ans;
    }
 
    // Driver Code
    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 } };
 
        // Function call
        int X[] = findMinArea(Mat, N, M);
        System.out.println(X[0] + " " + X[1]);
    }
}
 
// This code is contributed by Rohit Pradhan

Python3




# Python code to implement the above approach
import sys
MAX = 1000;
 
# Find M and N for which in every N*M
# area the max element remains same
def findMinArea(Mat, N, M):
    # Initialize the variable
    r = 0; c = 0;
    Max = -sys.maxsize -1;
 
    # Find position of maximum element
    # from the grid
    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)];
     
    # Return minimum value of submatrix
    ans[0] = max(r + 1, N - r);
    ans[1] = max(c + 1, M - c);
    return ans;
 
# Driver Code
if __name__ == '__main__':
    N = 4; M = 4;
    Mat = [[ 9, 12, 6, 11 ],
    [ 3, 15, 19, 4 ],
    [ 1, 13, 8, 11 ],
    [ 14, 7, 9, 15  ]];
 
    # Function call
    X = findMinArea(Mat, N, M);
    print(X[0] , " " , X[1]);
     
# This code is contributed by shikhasingrajput

C#




// Java code to implement the above approach
using System;
 
public class GFG {
   
    // Find M and N for which in every N*M
    // area the max element remains same
    public static int[] findMinArea(int[, ] Mat, int N,
                                    int M)
    {
        // Initialize the variable
        int r = 0, c = 0;
        int Max = Int32.MinValue;
 
        // Find position of maximum element
        // from the grid
        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];
        // Return minimum value of  submatrix
        ans[0] = Math.Max(r + 1, N - r);
        ans[1] = Math.Max(c + 1, M - c);
        return ans;
    }
 
    // Driver Code
    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 } };
 
        // Function call
        int[] X = findMinArea(Mat, N, M);
        Console.WriteLine(X[0] + " " + X[1]);
    }
}
 
// This code is contributed by Dharanendra L V.

Javascript




<script>
 
const MAX = 1000;
// Find M and N for which in every N*M
// area the max element remains same
function findMinArea(Mat, N, M)
{
        // Initialize the variable
        let r = 0, c = 0;
        let Max = Number.MIN_VALUE;
 
        // Find position of maximum element
        // from the grid
        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);
    // Return minimum value of  submatrix
    ans[0] = Math.max(r + 1, N - r);
    ans[1] = Math.max(c + 1, M - c);
    return ans;
}
 
// Driver Code
 
let N = 4, M = 4;
let Mat = [ [ 9, 12, 6, 11 ],
            [ 3, 15, 19, 4 ],
            [ 1, 13, 8, 11 ],
            [ 14, 7, 9, 15 ] ];
 
// Function call
let X = findMinArea(Mat, N, M);
document.write(X[0] + " " + X[1]);
 
// This code is contributed by shinjanpatra
 
</script>

Output

3 3

Time Complexity: O(N * M)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!