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

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

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


print-the-elements-of-a-matrix-in-spiral-form



print-the-elements-of-a-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



print-the-elements-of-a-matrix-in-spiral-form


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, Flipkartn


@@coding::1@@


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



print-the-elements-of-a-matrix-in-spiral-form


Recommended Programs









print-the-elements-of-a-matrix-in-spiral-form

c