Print all non-boundary elements of Matrix
Last Updated :
28 Dec, 2023
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++
#include <iostream>
using namespace std;
void printNonBoundary( int matrix[4][4], int n, int m)
{
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
if (i == 0 || j == 0 || i == m - 1
|| j == n - 1) {
continue ;
}
else {
cout << matrix[i][j] << " ";
}
}
cout << endl;
}
}
int main()
{
int matrix[4][4] = { { 8, 9, 0, 1 },
{ 2, 3, 6, 8 },
{ 3, 2, 5, 3 },
{ 2, 7, 5, 4 } };
printNonBoundary(matrix, 4, 4);
return 0;
}
|
Java
public class Main {
static void printNonBoundary( int [][] matrix, int n,
int m)
{
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
if (i == 0 || j == 0 || i == n - 1
|| j == m - 1 ) {
continue ;
}
else {
System.out.print(matrix[i][j] + " ");
}
}
System.out.println();
}
}
public static void main(String[] args)
{
int [][] matrix = { { 8 , 9 , 0 , 1 },
{ 2 , 3 , 6 , 8 },
{ 3 , 2 , 5 , 3 },
{ 2 , 7 , 5 , 4 } };
printNonBoundary(matrix, 4 , 4 );
}
}
|
Python3
def print_non_boundary(matrix):
n = len (matrix)
m = len (matrix[ 0 ])
for i in range (n):
for j in range (m):
if i = = 0 or j = = 0 or i = = n - 1 or j = = m - 1 :
continue
else :
print (matrix[i][j], end = " " )
print ()
if __name__ = = "__main__" :
matrix = [
[ 8 , 9 , 0 , 1 ],
[ 2 , 3 , 6 , 8 ],
[ 3 , 2 , 5 , 3 ],
[ 2 , 7 , 5 , 4 ]
]
print_non_boundary(matrix)
|
C#
using System;
class GFG
{
static void Geek( int [,] matrix, int n, int m)
{
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
if (i == 0 || j == 0 || i == n - 1 || j == m - 1)
{
continue ;
}
else
{
Console.Write(matrix[i, j] + " " );
}
}
Console.WriteLine();
}
}
public static void Main( string [] args)
{
int [,] matrix = {
{ 8, 9, 0, 1 },
{ 2, 3, 6, 8 },
{ 3, 2, 5, 3 },
{ 2, 7, 5, 4 }
};
Geek(matrix, 4, 4);
}
}
|
Javascript
function GFG(matrix, n, m) {
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
if (i === 0 || j === 0 || i === n - 1 || j === m - 1) {
continue ;
} else {
process.stdout.write(matrix[i][j] + " " );
}
}
console.log();
}
}
const matrix = [
[8, 9, 0, 1],
[2, 3, 6, 8],
[3, 2, 5, 3],
[2, 7, 5, 4]
];
GFG(matrix, 4, 4);
|
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.
Share your thoughts in the comments
Please Login to comment...