Related Articles
Java Program to Print Boundary Elements of the Matrix
• Last Updated : 04 Jan, 2021

Given a matrix of size Row x Col Print the boundary elements of the matrix. Boundary elements are those elements which are not surrounded by elements on all four directions, i.e. elements in the first row, first column, last row and last column.

Example:

```Input :
1 2 3
4 5 6
7 8 9
Output:
1 2 3
4   6
7 8 9```

Approach:

1. Take the matrix as input from the user of N × M dimension.
2. Print the border elements of the matrix using for loop.

Below is the implementation of the problem statement:

## Java

 `// Java Program to Print Boundary``// Elements of the Matrix`` ` `import` `java.util.*;`` ` `public` `class` `GFG {`` ` `    ``public` `void` `Boundary_Elements(``int` `mat[][])``    ``{``        ``// Printing Input Matrix;``        ``System.out.println(``"Input Matrix is : "``);`` ` `        ``for` `(``int` `i = ``0``; i < mat.length; i++) {``            ``for` `(``int` `j = ``0``; j < mat[i].length; j++) {`` ` `                ``System.out.print(mat[i][j]);``            ``}`` ` `            ``System.out.println();``        ``}`` ` `        ``// Printing Boundary Values of Matrix``        ``System.out.println(``"Resultant Matrix is :"``);`` ` `        ``for` `(``int` `i = ``0``; i < mat.length; i++) {``            ``for` `(``int` `j = ``0``; j < mat[i].length; j++) {`` ` `                ``if` `(i == ``0` `|| j == ``0` `|| i == mat.length - ``1``                    ``|| j == mat[i].length - ``1``) {``                    ``System.out.print(mat[i][j]);``                ``}``                ``else` `{``                    ``// Printing Space if element``                    ``// is not at Boundary``                    ``System.out.print(``" "``);``                ``}``            ``}`` ` `            ``System.out.println();``        ``}``    ``}``    ``public` `static` `void` `main(String[] args)``    ``{``        ``// Input Matrix``        ``int` `mat[][] = ``new` `int``[][] { { ``1``, ``2``, ``3` `},``                                    ``{ ``4``, ``5``, ``6` `},``                                    ``{ ``7``, ``8``, ``9` `} };`` ` `        ``GFG B_Values = ``new` `GFG();`` ` `        ``B_Values.Boundary_Elements(mat);``    ``}``}`
Output

```Input Matrix is :
123
456
789
Resultant Matrix is :
123
4 6
789
```

Time Complexity: O(N × M), where n and m are the dimensions of the matrix.

Space Complexity: O(N × M).

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up