Tag Archives: 2d array spiral printing

Print given square matrix in spiral order

Print givensquare matrix in spiral order :

Complexity : O(n^2)

Approach :

Step1 : write a 4 for loop for printing boundry

step 2: Apply the step1 for inner martrix by modifying starting point and ending point of matrix

step 3: Aplly step2 untill the matrix size becomes 1 or 0

end

Input :

—————-

1     2     3    4

12  13  14  5

11   16 15   6

10   9     8    7

—————-

Output :

——————————————————-

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

——————————————————-

C code to print given square matrix in spiral order :