Print spiral

Given a 2D array, print it in spiral form. That is, first you need to print the 1st row, then last column, then last row and then first column and so on.

Print every element only once.

Input format :

Line 1 : No. of rows & No. of columns (separated by space)

Line 2 : Row 1 elements (separated by space)

Line 3 : Row 2 elements (separated by space)

Line 4 : and so on

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

Login to submit your solution

Fb dab53a1a64929320dc7425896aa73fd9e5512b6ab52e414ff5f3ebb5cecc73eb

Share Problem