Print Kth boundary of a Matrix
Given a square matrix mat[][] and a positive integer K. The task is to print the Kth boundary of mat[][].
Examples:
Input: mat[][] = {{1, 2, 3, 4, 5}, K = 1
{6, 7, 8, 9, 10}
{11, 12, 13, 14, 15}
{16, 17, 18, 19, 20}
{21, 22, 23, 24, 25}}
Output: 1 2 3 4 5
6 10
11 15
16 20
21 22 23 24 25
Explanation: The first boundary of mat[][] is above.
Input: mat[][] = {{1, 2, 3}, K = 2
{4, 5, 6}
{7, 8, 9}}
Output: 5
Approach: This problem is implementation-based. Traverse the matrix and check for every element if that element lies on the Kth boundary or not. If yes then print the element else print space character. Follow the steps below to solve the given problem.
- for i in from 0 to N
- for j in from 0 to N
- if((i == K – 1 or i == N – K) and (j >= K – 1 and j <= N – K))
- else if (j == K – 1 or j == N – K) and (i >= K – 1 and i <= N – K):
- This will give the required Kth border of mat[][]
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void printKthBorder(vector<vector< int >> mat, int N, int K)
{
for ( int i = 0; i < N; i++)
{
cout << endl;
for ( int j = 0; j < N; j++)
{
int flag = 0;
if ((i == K - 1 || i == N - K) &&
(j >= K - 1 && j <= N - K)) {
cout << mat[i][j] << " " ;
flag = 1;
}
else if ((j == K - 1 || j == N - K) &&
(i >= K - 1 && i <= N - K)) {
cout << mat[i][j] << " " ;
flag = 1;
}
if (flag == 0)
cout << " " ;
}
}
}
int main() {
int N = 5;
int K = 1;
vector<vector< int >> mat = {{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}};
printKthBorder(mat, N, K);
}
|
Java
import java.util.*;
public class GFG
{
static void printKthBorder( int [][]mat, int N, int K)
{
for ( int i = 0 ; i < N; i++)
{
System.out.println();
for ( int j = 0 ; j < N; j++)
{
int flag = 0 ;
if ((i == K - 1 || i == N - K) &&
(j >= K - 1 && j <= N - K)) {
System.out.print(mat[i][j] + " " );
flag = 1 ;
}
else if ((j == K - 1 || j == N - K) &&
(i >= K - 1 && i <= N - K)) {
System.out.print(mat[i][j] + " " );
flag = 1 ;
}
if (flag == 0 )
System.out.print( " " );
}
}
}
public static void main(String args[]) {
int N = 5 ;
int K = 1 ;
int [][]mat = {{ 1 , 2 , 3 , 4 , 5 },
{ 6 , 7 , 8 , 9 , 10 },
{ 11 , 12 , 13 , 14 , 15 },
{ 16 , 17 , 18 , 19 , 20 },
{ 21 , 22 , 23 , 24 , 25 }};
printKthBorder(mat, N, K);
}
}
|
Python3
def printKthBorder(mat, N, K):
for i in range (N):
print ()
for j in range (N):
flag = 0
if ((i = = K - 1 or i = = N - K) \
and (j > = K - 1 and j < = N - K)):
print (mat[i][j], end = " " )
flag = 1
elif (j = = K - 1 or j = = N - K) \
and (i > = K - 1 and i < = N - K):
print (mat[i][j], end = " " )
flag = 1
if flag = = 0 :
print (end = " " )
N = 5
K = 1
mat = [[ 1 , 2 , 3 , 4 , 5 ], \
[ 6 , 7 , 8 , 9 , 10 ], \
[ 11 , 12 , 13 , 14 , 15 ],
[ 16 , 17 , 18 , 19 , 20 ], \
[ 21 , 22 , 23 , 24 , 25 ]]
printKthBorder(mat, N, K)
|
C#
using System;
class GFG
{
static void printKthBorder( int [,]mat, int N, int K)
{
for ( int i = 0; i < N; i++)
{
Console.WriteLine();
for ( int j = 0; j < N; j++)
{
int flag = 0;
if ((i == K - 1 || i == N - K) &&
(j >= K - 1 && j <= N - K)) {
Console.Write(mat[i, j] + " " );
flag = 1;
}
else if ((j == K - 1 || j == N - K) &&
(i >= K - 1 && i <= N - K)) {
Console.Write(mat[i, j] + " " );
flag = 1;
}
if (flag == 0)
Console.Write( " " );
}
}
}
public static void Main() {
int N = 5;
int K = 1;
int [,]mat = {{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}};
printKthBorder(mat, N, K);
}
}
|
Javascript
<script>
function printKthBorder(mat, N, K)
{
for (let i = 0; i < N; i++)
{
document.write( '<br>' )
for (let j = 0; j < N; j++)
{
flag = 0
if ((i == K - 1 || i == N - K) &&
(j >= K - 1 && j <= N - K)) {
document.write(mat[i][j] + " " )
flag = 1
}
else if ((j == K - 1 || j == N - K) &&
(i >= K - 1 && i <= N - K)) {
document.write(mat[i][j] + " " )
flag = 1
}
if (flag == 0)
document.write( " " );
}
}
}
N = 5
K = 1
mat = [[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25]]
printKthBorder(mat, N, K)
</script>
|
Output
1 2 3 4 5
6 10
11 15
16 20
21 22 23 24 25
Time Complexity: O(N^2)
Space Complexity: O(1)
Last Updated :
07 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...