Find maximum of minimums from Layers of Matrix using numbers 1 to N^2

Given a square matrix of size N*N using numbers 1 to N^2, the task is to find the maximum of minimums of each layer of the matrix.

The layers of the matrix are the boundary elements of the sub-matrix starting at (i, i) and ending at (N – i + 1, N – i + 1), where 1<= i<= ceil(N/2).

Examples:

 Input: Below is the given matrix:
1      2     3     4
5     6     7     8
9    10   11    12
13  14   15    16
Output: 6
Explanation: The layers are {1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5} with minimum 1 and {6, 7, 10, 11} with minimum 6. The maximum of 1 and 6 is 6.

Input: Below is the given matrix: 
1    2    3
4   30   5
1   2     3
Output: 30
Explanation: The layers are {1, 2, 3, 5, 3, 2, 1, 4, 1} with minimum 1 and {30} with minimum 30. The maximum of 1 and 30 is 30.



 

Approach: The idea is to observe carefully, for the ith layer, the elements of the top, left, right and bottom boundary are at indexes:

  • top boundary is at indexes (i, j)
  • left boundary is at indexes (j, i)
  • right boundary is at indexes (j, n – i + 1)
  • bottom boundary is at indexes (n – i + 1, j), where i <= j <= n – i + 1

Thus, find the minimums of each layer and store the maximum of these minimums.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include<bits/stdc++.h>
using namespace std;
  
// Function to return the minimum
// of the boundary elements
int getBoundaryMin(int a[][4], int n,
                   int index)
{
    int min1 = INT_MAX;
  
    for(int i = index; i < n - index; i++) 
    {
          
        // Top boundary
        min1 = min(min1,
                   a[index][i]);
  
        // Left boundary
        min1 = min(min1,
                   a[i][index]);
  
        // Right boundary
        min1 = min(min1,
                    a[i][n - index - 1]);
  
        // Bottom boundary
        min1 = min(min1,
                   a[n - index - 1][i]);
    }
    return min1;
}
  
// Function to find the maximum of
// minimums of all layers
void MaximumOfMinimum(int a[][4], int n)
{
      
    // Calculate the layers
    int layers = n / 2 + n % 2;
    int max1 = INT_MIN;
  
    // Iterate for all the layers
    for(int i = 0; i < layers; i++)
    {
          
        // Find maximum
        max1 = max(max1,
                   getBoundaryMin(a, n, i));
    }
      
    // Print the answer
    cout << (max1);
}
  
// Driver Code
int main()
{
      
    // Initialize the matrix
    int a[][4] = { { 1, 2, 3, 4 },
                   { 5, 6, 7, 8 },
                   { 9, 10, 11, 12 },
                   { 13, 14, 15, 16 } };
  
    int n = sizeof(a) / sizeof(a[0]);
  
    // Function call
    MaximumOfMinimum(a, n);
}
  
// This code is contributed by chitranayal

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program for the above approach
  
class GFG {
  
    // Function to return the minimum
    // of the boundary elements
    static int
    getBoundaryMin(int a[][], int n,
                   int index)
    {
        int min = Integer.MAX_VALUE;
  
        for (int i = index; i < n - index; i++) {
  
            // Top boundary
            min = Math.min(
                min,
                a[index][i]);
  
            // Left boundary
            min = Math.min(
                min,
                a[i][index]);
  
            // Right boundary
            min = Math.min(
                min,
                a[i][n - index - 1]);
  
            // Bottom boundary
            min = Math.min(
                min,
                a[n - index - 1][i]);
        }
  
        return min;
    }
  
    // Function to find the maximum of
    // minimums of all layers
    static void MaximumOfMinimum(
        int a[][], int n)
    {
        // Calculate the layers
        int layers = n / 2 + n % 2;
        int max = Integer.MIN_VALUE;
  
        // Iterate for all the layers
        for (int i = 0; i < layers; i++) {
  
            // Find maximum
            max
                = Math.max(
                    max,
                    getBoundaryMin(a, n, i));
        }
  
        // Print the answer
        System.out.print(max);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        // Initialize the matrix
        int a[][] = { { 1, 2, 3, 4 },
                      { 5, 6, 7, 8 },
                      { 9, 10, 11, 12 },
                      { 13, 14, 15, 16 } };
  
        int n = a.length;
  
        // Function call
        MaximumOfMinimum(a, n);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
  
class GFG{
  
// Function to return the minimum
// of the boundary elements
static int getBoundaryMin(int[,]a, int n,
                          int index)
{
    int min = int.MaxValue;
  
    for(int i = index; i < n - index; i++) 
    {
          
        // Top boundary
        min = Math.Min(min, a[index, i]);
  
        // Left boundary
        min = Math.Min(min, a[i, index]);
  
        // Right boundary
        min = Math.Min(min, a[i, n - index - 1]);
  
        // Bottom boundary
        min = Math.Min(min, a[n - index - 1, i]);
    }
    return min;
}
  
// Function to find the maximum of
// minimums of all layers
static void MaximumOfMinimum(int[,]a, int n)
{
      
    // Calculate the layers
    int layers = n / 2 + n % 2;
    int max = int.MinValue;
  
    // Iterate for all the layers
    for(int i = 0; i < layers; i++) 
    {
          
        // Find maximum
        max = Math.Max(max,
                       getBoundaryMin(a, n, i));
    }
  
    // Print the answer
    Console.Write(max);
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Initialize the matrix
    int[,]a = { { 1, 2, 3, 4 },
                { 5, 6, 7, 8 },
                { 9, 10, 11, 12 },
                { 13, 14, 15, 16 } };
  
    int n = a.GetLength(0);
  
    // Function call
    MaximumOfMinimum(a, n);
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output: 

6

Time Complexity: O(N2)
Auxilary Space: O(1)

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : chitranayal, 29AjayKumar