Given a matrix of custom size the task is to print the maximum non-boundary element.
Example: Consider a matrix:-
| 11 | 4 | 5 | 17 |
| 3 | 10 | 12 | 3 |
| 5 | 6 | 11 | 9 |
Here maximum non-boundary elements is 12.
Java
import java.util.*;
public class MaxNonBoundaryElement
{
public static void main(String args[])
{
int row=0,col=0,max=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter no.of rows: ");
row=sc.nextInt();
System.out.print("Enter no.of columns: ");
col=sc.nextInt();
int ar[][]=new int[row][col];
System.out.println("Enter matrix elements:-");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
ar[i][j]=sc.nextInt();
}
}
if(row<=2&&col<=2)
{
System.out.println("Non-boundary elements does not exists");
System.exit(1);
}
max=ar[1][1];
for(int i=1;i<row-1;i++)
{
for(int j=1;j<col-1;j++)
{
if(max<ar[i][j])
{
max=ar[i][j];
}
}
}
System.out.println("Maximum non-boundary elements is "+max);
}
}Java