Print Kth boundary of a Matrix
Last Updated :
07 Dec, 2021
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++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to print Kth border of a matrix
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++)
{
// To keep track of which element to skip
int flag = 0;
if ((i == K - 1 || i == N - K) &&
(j >= K - 1 && j <= N - K)) {
// Print the element
cout << mat[i][j] << " ";
flag = 1;
}
else if ((j == K - 1 || j == N - K) &&
(i >= K - 1 && i <= N - K)) {
// Print the element
cout << mat[i][j] << " ";
flag = 1;
}
if (flag == 0)
cout << " ";
}
}
}
// Driver code
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);
}
// This code is contributed by Samim Hossain Mondal.
Java
// Java Program to implement
// the above approach
import java.util.*;
public class GFG
{
// Function to print Kth border of a matrix
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++)
{
// To keep track of which element to skip
int flag = 0;
if ((i == K - 1 || i == N - K) &&
(j >= K - 1 && j <= N - K)) {
// Print the element
System.out.print(mat[i][j] + " ");
flag = 1;
}
else if ((j == K - 1 || j == N - K) &&
(i >= K - 1 && i <= N - K)) {
// Print the element
System.out.print(mat[i][j] + " ");
flag = 1;
}
if (flag == 0)
System.out.print(" ");
}
}
}
// Driver code
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);
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python program for above approach
# Function to print Kth border of a matrix
def printKthBorder(mat, N, K):
for i in range(N):
print()
for j in range(N):
# To keep track of which element to skip
flag = 0
if((i == K-1 or i == N-K) \
and (j >= K-1 and j <= N-K)):
# Print the element
print(mat[i][j], end =" ")
flag = 1
elif (j == K-1 or j == N-K) \
and (i >= K-1 and i <= N-K):
# Print the element
print(mat[i][j], end =" ")
flag = 1
if flag == 0:
print(end =" ")
# Driver code
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#
// C# Program to implement
// the above approach
using System;
class GFG
{
// Function to print Kth border of a matrix
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++)
{
// To keep track of which element to skip
int flag = 0;
if ((i == K - 1 || i == N - K) &&
(j >= K - 1 && j <= N - K)) {
// Print the element
Console.Write(mat[i, j] + " ");
flag = 1;
}
else if ((j == K - 1 || j == N - K) &&
(i >= K - 1 && i <= N - K)) {
// Print the element
Console.Write(mat[i, j] + " ");
flag = 1;
}
if (flag == 0)
Console.Write(" ");
}
}
}
// Driver code
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);
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
<script>
// JavaScript Program to implement
// the above approach
// Function to print Kth border of a matrix
function printKthBorder(mat, N, K)
{
for (let i = 0; i < N; i++)
{
document.write('<br>')
for (let j = 0; j < N; j++)
{
// To keep track of which element to skip
flag = 0
if ((i == K - 1 || i == N - K) &&
(j >= K - 1 && j <= N - K)) {
// Print the element
document.write(mat[i][j] + " ")
flag = 1
}
else if ((j == K - 1 || j == N - K) &&
(i >= K - 1 && i <= N - K)) {
// Print the element
document.write(mat[i][j] + " ")
flag = 1
}
if (flag == 0)
document.write(" ");
}
}
}
// Driver code
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)
// This code is contributed by Potta Lokesh
</script>
Output1 2 3 4 5
6 10
11 15
16 20
21 22 23 24 25
Time Complexity: O(N^2)
Space Complexity: O(1)
Similar Reads
Print all non-boundary elements of Matrix 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 so
5 min read
Boundary elements of a Matrix Given a matrix mat[][] of size n*m, the task is to traverse the boundary elements in clockwise order, starting from the top-left element.Examples: Input: mat[][] = [[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12] ]Output : 1 2 3 4 8 12 11 10 9 5 Input: mat[][] = [ [1, 2], [3, 4]]Output: 1 2 4 3Approach: T
10 min read
Boundary elements of a Matrix Given a matrix mat[][] of size n*m, the task is to traverse the boundary elements in clockwise order, starting from the top-left element.Examples: Input: mat[][] = [[1, 2, 3, 4],[5, 6, 7, 8],[9, 10, 11, 12] ]Output : 1 2 3 4 8 12 11 10 9 5 Input: mat[][] = [ [1, 2], [3, 4]]Output: 1 2 4 3Approach: T
10 min read
Print sum of matrix and its mirror image You are given a matrix of order N*N. The task is to find the resultant matrix by adding the mirror image of given matrix with the matrix itself. Examples: Input : mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Output : 4 4 4 10 10 10 16 16 16 Explanation: Resultant Matrix = {{1, 2, 3}, {{3, 2, 1}, {4,
5 min read
Print matrix in diagonal pattern Given a matrix of n*n size, the task is to print its elements in a diagonal pattern. Input : mat[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Output : 1 2 4 7 5 3 6 8 9. Explanation: Start from 1 Then from upward to downward diagonally i.e. 2 and 4 Then from downward to upward diagonally i.e 7, 5, 3 Th
14 min read
Diagonal Traversal of a Matrix I Given a 2D matrix of size n*m, the tasks is to print all elements of the given matrix in diagonal order.Example:Input: mat[][] = [[ 1, 2, 3, 4 ], [5, 6, 7, 8 ], [9, 10, 11, 12], [13, 14, 15, 16], [17, 18, 19, 20]]Output: 1 5 2 9 6 3 13 10 7 4 17 14 11 8 18 15 12 19 16 20 Using Line by Line Diagonal
13 min read