VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-given-matrix-counter-clock-wise-spiral-form/

⇱ Print a given matrix in counter-clock wise spiral form - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print a given matrix in counter-clock wise spiral form

Last Updated : 5 Dec, 2022

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 : 
 

👁 Image


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 : 
 


Output
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.

Comment
Article Tags:
Article Tags: