Wednesday, July 3, 2024
HomeLanguagesJavaJava Program to Print Boundary Elements of the Matrix

Java Program to Print Boundary Elements of the Matrix

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).

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments