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.
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
In order to print a matrix in spiral form, you need to follow the below approach.
Asked in recruitment drives of companies like Paypal, SapLabs, Flipkartn
@@coding::1@@
The complexity of spiral matrix printing: O(M*N)