Open In App

Print Kth boundary of a Matrix

Improve
Improve
Like Article
Like
Save
Share
Report

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))
        • print mat[i][j]
      • else if (j == K – 1 or j == N – K) and (i >= K – 1 and i <= N – K):
        • print mat[i][j]
  • 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>


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
Previous
Next
Share your thoughts in the comments
Similar Reads