Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmPrint Kth boundary of a Matrix

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))
        • 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)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
07 Dec, 2021
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments