VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-to-print-boundary-elements-of-a-matrix/

⇱ C Program to Print Boundary Elements of a Matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C Program to Print Boundary Elements of a Matrix

Last Updated : 7 Oct, 2022

Here, we will print the boundary elements of a matrix using a C program:

Input :
 1 2 3 4 
 1 2 3 4
 1 2 3 4
 1 2 3 4
Output : 
 1 2 3 4 
 1 4 
 1 4 
 1 2 3 4

Approach:

  • Traverse the matrix from start to end.
  • Assign an outer loop to point to the row and the inner row to traverse the elements of the row.
  • if the element lies on the boundary (1st row, 1st column, last row, last column), then print the element, else print a blank space.

Example:


Output
 Input Matrix 
 1 2 3 4 
 1 2 3 4 
 1 2 3 4 
 1 2 3 4 

 Boundary of Matrix 
 1 2 3 4
 1 4
 1 4
 1 2 3 4

Time Complexity: O(n*n), where n is the size of the array.

Space Complexity: O(1). 

Comment
Article Tags: