Find maximum of minimums from Layers of Matrix using numbers 1 to N^2
Last Updated :
06 Jun, 2021
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++
#include<bits/stdc++.h>
using namespace std;
int getBoundaryMin( int a[][4], int n,
int index)
{
int min1 = INT_MAX;
for ( int i = index; i < n - index; i++)
{
min1 = min(min1,
a[index][i]);
min1 = min(min1,
a[i][index]);
min1 = min(min1,
a[i][n - index - 1]);
min1 = min(min1,
a[n - index - 1][i]);
}
return min1;
}
void MaximumOfMinimum( int a[][4], int n)
{
int layers = n / 2 + n % 2;
int max1 = INT_MIN;
for ( int i = 0; i < layers; i++)
{
max1 = max(max1,
getBoundaryMin(a, n, i));
}
cout << (max1);
}
int main()
{
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]);
MaximumOfMinimum(a, n);
}
|
Java
class GFG {
static int
getBoundaryMin( int a[][], int n,
int index)
{
int min = Integer.MAX_VALUE;
for ( int i = index; i < n - index; i++) {
min = Math.min(
min,
a[index][i]);
min = Math.min(
min,
a[i][index]);
min = Math.min(
min,
a[i][n - index - 1 ]);
min = Math.min(
min,
a[n - index - 1 ][i]);
}
return min;
}
static void MaximumOfMinimum(
int a[][], int n)
{
int layers = n / 2 + n % 2 ;
int max = Integer.MIN_VALUE;
for ( int i = 0 ; i < layers; i++) {
max
= Math.max(
max,
getBoundaryMin(a, n, i));
}
System.out.print(max);
}
public static void main(String[] args)
{
int a[][] = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 } };
int n = a.length;
MaximumOfMinimum(a, n);
}
}
|
Python3
import sys
def getBoundaryMin(a, n, index):
min1 = sys.maxsize
for i in range (index, n - index):
min1 = min (min1, a[index][i])
min1 = min (min1, a[i][index])
min1 = min (min1, a[i][n - index - 1 ])
min1 = min (min1, a[n - index - 1 ][i])
return min1
def MaximumOfMinimum(a, n):
layers = n / / 2 + n % 2
max1 = - sys.maxsize - 1
for i in range ( 0 , layers):
max1 = max (max1, getBoundaryMin(a, n, i))
print (max1)
a = [ [ 1 , 2 , 3 , 4 ],
[ 5 , 6 , 7 , 8 ],
[ 9 , 10 , 11 , 12 ] ,
[ 13 , 14 , 15 , 16 ] ]
n = len (a)
MaximumOfMinimum(a, n)
|
C#
using System;
class GFG{
static int getBoundaryMin( int [,]a, int n,
int index)
{
int min = int .MaxValue;
for ( int i = index; i < n - index; i++)
{
min = Math.Min(min, a[index, i]);
min = Math.Min(min, a[i, index]);
min = Math.Min(min, a[i, n - index - 1]);
min = Math.Min(min, a[n - index - 1, i]);
}
return min;
}
static void MaximumOfMinimum( int [,]a, int n)
{
int layers = n / 2 + n % 2;
int max = int .MinValue;
for ( int i = 0; i < layers; i++)
{
max = Math.Max(max,
getBoundaryMin(a, n, i));
}
Console.Write(max);
}
public static void Main(String[] args)
{
int [,]a = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
int n = a.GetLength(0);
MaximumOfMinimum(a, n);
}
}
|
Javascript
<script>
function getBoundaryMin(a, n, index)
{
min1 = 1000000000;
for ( var i = index; i < n - index; i++)
{
min1 = Math.min(min1,
a[index][i]);
min1 = Math.min(min1,
a[i][index]);
min1 = Math.min(min1,
a[i][n - index - 1]);
min1 = Math.min(min1,
a[n - index - 1][i]);
}
return min1;
}
function MaximumOfMinimum(a, n)
{
var layers = parseInt(n / 2) + n % 2;
var max1 = -1000000000;
for ( var i = 0; i < layers; i++)
{
max1 = Math.max(max1,
getBoundaryMin(a, n, i));
}
document.write(max1);
}
var a = [ [ 1, 2, 3, 4 ],
[ 5, 6, 7, 8 ],
[ 9, 10, 11, 12 ],
[ 13, 14, 15, 16 ] ];
var n = a.length;
MaximumOfMinimum(a, n);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...