Spirally traversing a matrix
Submissions: 20555   Accuracy:

35.07%

  Difficulty: Easy

Given a 2D matrix of size M*N. Traverse and print the matrix in spiral form.

Input:
The first line of the input contains a single integer T, denoting the number of test cases. Then T test cases follow. Each testcase has 2 lines. First line contains M and N respectively separated by a space. Second line contains M*N values separated by spaces.

Output:
Elements when travelled in Spiral form, will be displayed in a single line.

Constraints:
1 <=T<= 100
2 <= M, N <= 10
0 <= Ai <= 100

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

Explanation:
Testcase 1: Refer this.

** For More Input/Output Examples Use 'Expected Output' option **

Author: atharv


Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.