![]() |
VOOZH | about |
Given a 2D array, print it in counter-clock wise spiral form. See the following examples.
Examples :
Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7 Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Output: 1 7 13 14 15 16 17 18 12 6 5 4 3 2 8 9 10 11
Explanation :
Below is the implementation :
Output :
1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7
Time Complexity : O(mn).
Auxiliary Space: O(1) because constant space has been used
Alternate Implementation :
1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7
Time Complexity: O(n2)
Auxiliary Space: O(1), since no extra space has been taken.