Open In App

Print all non-boundary elements of Matrix

Last Updated : 28 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an N x M matrix, print all its non-boundary elements. (all elements except boundary elements)

Examples:

Input: {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}
Output: 6 7

Input: {{8, 7, 6, 5, 4, 8}, {3, 5, 2, 1, 7, 9}, {6, 7, 3, 9, 0, 7}, {3, 6, 9, 2, 1, 0}}
Output: 5 2 1 7
7 3 9 0

Approach: To solve the problem follow the below idea:

Traverse the whole matrix through the nested loop. When matrix[i][j] points to the boundary elements then don’t print it, else print the elements.

Steps to solve the problem using the approach:

  • Traverse the matrix by using two loops.
  • The outer loop points to the row number and the inner loop traverses through all elements
  • If it is a boundary element then use continue.
  • else print the elements.

Implementation of the above approach:

C++




// C++ code for the above approach:
#include <iostream>
using namespace std;
 
void printNonBoundary(int matrix[4][4], int n, int m)
{
    // outer loop
    for (int i = 0; i < n; i++) {
        // inner loop
        for (int j = 0; j < m; j++) {
            if (i == 0 || j == 0 || i == m - 1
                || j == n - 1) {
                // don't print if boundary element
                continue;
            }
            else {
               
                // if it is not boundary element,
                // then print it
                cout << matrix[i][j] << " ";
            }
        }
        cout << endl;
    }
}
 
// Drivers code
int main()
{
   
    // Matrix elements
    int matrix[4][4] = { { 8, 9, 0, 1 },
                         { 2, 3, 6, 8 },
                         { 3, 2, 5, 3 },
                         { 2, 7, 5, 4 } };
 
    // Function call
    printNonBoundary(matrix, 4, 4);
 
    return 0;
}


Java




public class Main {
    // Function to print non-boundary elements of a matrix
    static void printNonBoundary(int[][] matrix, int n,
                                 int m)
    {
        // Outer loop for rows
        for (int i = 0; i < n; i++) {
            // Inner loop for columns
            for (int j = 0; j < m; j++) {
                // Check if the element is on the boundary
                if (i == 0 || j == 0 || i == n - 1
                    || j == m - 1) {
                    // Don't print if it's a boundary
                    // element
                    continue;
                }
                else {
                    // Print the non-boundary element
                    System.out.print(matrix[i][j] + " ");
                }
            }
            // Move to the next line after printing a row
            System.out.println();
        }
    }
 
    public static void main(String[] args)
    {
        // Define a 4x4 matrix
        int[][] matrix = { { 8, 9, 0, 1 },
                           { 2, 3, 6, 8 },
                           { 3, 2, 5, 3 },
                           { 2, 7, 5, 4 } };
 
        // Call the function to print non-boundary elements
        printNonBoundary(matrix, 4, 4);
    }
}


Python3




def print_non_boundary(matrix):
    n = len(matrix)
    m = len(matrix[0])
     
    # Outer loop
    for i in range(n):
        # Inner loop
        for j in range(m):
            if i == 0 or j == 0 or i == n - 1 or j == m - 1:
                # Don't print if it's a boundary element
                continue
            else:
                # If it's not a boundary element, then print it
                print(matrix[i][j], end=" ")
        print()  # Move to the next line after printing each row
 
 
# Driver code
if __name__ == "__main__":
    # Matrix elements
    matrix = [
        [8, 9, 0, 1],
        [2, 3, 6, 8],
        [3, 2, 5, 3],
        [2, 7, 5, 4]
    ]
 
    # Function call
    print_non_boundary(matrix)
 
# This code is contributed by shivamgupta0987654321


C#




using System;
 
class GFG
{
    static void Geek(int[,] matrix, int n, int m)
    {
        // Outer loop for rows
        for (int i = 0; i < n; i++)
        {
            // Inner loop for columns
            for (int j = 0; j < m; j++)
            {
                if (i == 0 || j == 0 || i == n - 1 || j == m - 1)
                {
                    continue;
                }
                else
                {
                    // If it is not a boundary element.
                    Console.Write(matrix[i, j] + " ");
                }
            }
            Console.WriteLine();
        }
    }
    // Driver code
    public static void Main(string[] args)
    {
        // Define a 4x4 matrix
        int[,] matrix = {
            { 8, 9, 0, 1 },
            { 2, 3, 6, 8 },
            { 3, 2, 5, 3 },
            { 2, 7, 5, 4 }
        };
        // Function call
        Geek(matrix, 4, 4);
    }
}


Javascript




function GFG(matrix, n, m) {
    // Outer loop for rows
    for (let i = 0; i < n; i++) {
        // Inner loop for columns
        for (let j = 0; j < m; j++) {
            // Check if the element is on the boundary
            if (i === 0 || j === 0 || i === n - 1 || j === m - 1) {
                // Don't print if it's a boundary element
                continue;
            } else {
                // Print the non-boundary element
                process.stdout.write(matrix[i][j] + " ");
            }
        }
        // Move to the next line after printing a row
        console.log();
    }
}
// Define a 4x4 matrix
const matrix = [
    [8, 9, 0, 1],
    [2, 3, 6, 8],
    [3, 2, 5, 3],
    [2, 7, 5, 4]
];
GFG(matrix, 4, 4);


Output

3 6 
2 5 





Time Complexity: O(n2) , as we are using nested loop to traverse the whole matrix.
Auxiliary Space: O(1) , as it taking only extra constant space.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads