Print boundary elements of a given matrix in clockwise manner
Given a matrix arr[][] of size N * M, the task is to print the boundary elements of the given matrix in a clockwise form.
Examples:
Input: arr[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9} }
Output: 1 2 3 6 9 8 7 4
Explanation:
Boundary elements of the matrix are:
1 2 3
4 5 6
7 8 <strong>9
Therefore, the sequence of boundary elements in clockwise form is {1, 2, 3, 6, 9, 8, 7, 4}.
Input: arr[][] = {{11, 12, 33}, {64, 57, 61}, {74, 88, 39}}
Output: 11 12 33 61 39 88 74 64
Naive Approach: The simplest approach to solve this problem is to traverse the given matrix and check if the current element is the boundary element or not. If found to be true, then print the element.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach, the idea is to traverse only the first and last rows and the first and last columns of the matrix. Follow the steps below to solve the problem:
- Print the first row of the matrix.
- Print the last column of the matrix except the first row.
- Print the last row of the matrix except the last column.
- Print the first column of the matrix except the first and last row.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void boundaryTraversal(vector<vector< int > > arr, int N,
int M)
{
for ( int i = 0; i < M; i++)
{
cout << arr[0][i] << " " ;
}
for ( int i = 1; i < N; i++)
{
cout << arr[i][M - 1] << " " ;
}
if (N > 1)
{
for ( int i = M - 2; i >= 0; i--)
{
cout << arr[N - 1][i] << " " ;
}
}
if (M > 1) {
for ( int i = N - 2; i > 0; i--) {
cout << arr[i][0] << " " ;
}
}
}
int main()
{
vector<vector< int > > arr{ { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int N = arr.size();
int M = arr[0].size();
boundaryTraversal(arr, N, M);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void boundaryTraversal(
int arr[][], int N, int M)
{
for ( int i = 0 ; i < M; i++) {
System.out.print(arr[ 0 ][i] + " " );
}
for ( int i = 1 ; i < N; i++) {
System.out.print(arr[i][M - 1 ] + " " );
}
if (N > 1 ) {
for ( int i = M - 2 ; i >= 0 ; i--) {
System.out.print(arr[N - 1 ][i] + " " );
}
}
if (M > 1 ) {
for ( int i = N - 2 ; i > 0 ; i--) {
System.out.print(arr[i][ 0 ] + " " );
}
}
}
public static void main(String[] args)
{
int arr[][]
= { { 1 , 2 , 3 },
{ 4 , 5 , 6 },
{ 7 , 8 , 9 } };
int N = arr.length;
int M = arr[ 0 ].length;
boundaryTraversal(arr, N, M);
}
}
|
Python3
def boundaryTraversal(arr, N, M):
for i in range (M):
print (arr[ 0 ][i], end = " " );
for i in range ( 1 , N):
print (arr[i][M - 1 ], end = " " );
if (N > 1 ):
for i in range (M - 2 , - 1 , - 1 ):
print (arr[N - 1 ][i], end = " " );
if (M > 1 ):
for i in range (N - 2 , 0 , - 1 ):
print (arr[i][ 0 ], end = " " );
if __name__ = = '__main__' :
arr = [[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]];
N = len (arr);
M = len (arr[ 0 ]);
boundaryTraversal(arr, N, M);
|
C#
using System;
class GFG{
static void boundaryTraversal( int [,] arr,
int N, int M)
{
for ( int i = 0; i < M; i++)
{
Console.Write(arr[0, i] + " " );
}
for ( int i = 1; i < N; i++)
{
Console.Write(arr[i, M - 1] + " " );
}
if (N > 1)
{
for ( int i = M - 2; i >= 0; i--)
{
Console.Write(arr[N - 1, i] + " " );
}
}
if (M > 1)
{
for ( int i = N - 2; i > 0; i--)
{
Console.Write(arr[i, 0] + " " );
}
}
}
static void Main()
{
int [,] arr = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int N = 3;
int M = 3;
boundaryTraversal(arr, N, M);
}
}
|
Javascript
<script>
function boundaryTraversal(arr, N, M)
{
for (let i = 0; i < M; i++)
{
document.write(arr[0][i] + " " );
}
for (let i = 1; i < N; i++)
{
document.write(arr[i][M - 1] + " " );
}
if (N > 1)
{
for (let i = M - 2; i >= 0; i--)
{
document.write(arr[N - 1][i] + " " );
}
}
if (M > 1) {
for (let i = N - 2; i > 0; i--) {
document.write(arr[i][0] + " " );
}
}
}
let arr = [ [ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ] ];
let N = arr.length;
let M = arr[0].length;
boundaryTraversal(arr, N, M);
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(1)
Last Updated :
26 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...