Explore
ProGrad Programs
Placement Prep
TCS Codevita
Live Placement Training
Webinars
About Us

Edit
Reply




Edit

Spiral Matrix Printing | Print the elements of a matrix in spiral form

Published on 09 Mar 2020

Program to print the elements of a 2d array in the form of a matrix in spiral form is discussed here. The below images gives you an idea of spiral order matrix printing.


matrix in spiral form


Test case:


Input: 3 6 //matrix size


1 2 3 4 5 6

7 8 9 10 11 12

13 14 15 16 17 18


Output:


1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11


Algorithm to print matrix in spiral form


In order to print a matrix in spiral form, you need to follow the below approach.


  • Left to right (first row)
  • Top to bottom (Last column)
  • Right to left (last row)
  • Bottom to top (First column)


Program to Print Matrix in spiral form


Asked in recruitment drives of companies like Paypal, SapLabs, Flipkart


C
C++
Java
Python 3

Output
Input- Input the number of rows:3 Input the number of columns:6 The Matrix is 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Output- Spiral Matrix : 1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11


The complexity of spiral matrix printing: O(M*N)


Recommended Programs






If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in