Print 2-D Array in clockwise expanding spiral from center
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Printing a 2-D array in a spiral order is a common programming challenge. Usually, the spiral traversal starts from the outermost elements moving inward. However, an intriguing twist to this problem is to start printing from the center of the matrix, expanding outwards in a clockwise spiral. In this article, we will delve into how to approach this unique method, offering technical details, algorithms, and considerations for optimal implementation.
Problem Explanation
Given a 2-D array (or matrix), the goal is to print its elements in a specific order: starting from the center and moving outward in a clockwise spiral. This is particularly interesting because it challenges the usual assumptions about finding spiral order from the array's perimeter.
Assumptions
- The matrix is non-empty.
- The dimensions of the matrix can be odd (e.g., 3x3) or even (e.g., 4x4).
- In the case of even dimensions, such as 4x4, consider one of the center elements as the starting point (e.g., element (m/2 - 1, n / 2 - 1)).
Algorithm Outline
To solve this problem, we can break it down into several steps:
- Identify the Center: Determine the starting point, which is the center of the matrix.
- Initialize Spiral Directions: Use vectors to signify the current direction of travel—right, down, left, and up.
- Edge Handling: Keep track of edges as the spiral expands and switch directions when the edge is reached.
- Store and Visit: Ensure each element is visited once and stored in an ordered sequence.
- Expand and Rotate: After finishing one circle, expand outwards and repeat until all elements are printed.
In pseudocode, this might look like:
1 2 3 4 5 6 7 8 9
- Single element matrices should directly return that element.
- Handling rectangular matrices may require additional checks on indices.
- This approach visits each element once, leading to a time complexity of where is the number of rows and is the number of columns.
- Space complexity is relatively minimal, primarily requiring a separate list to store the order and a set for visited checks.
Related reading
- Print a polynomial using minimum number of calls
- Print all day-dates between two dates
- Print all numbers whose nonzero digits are in ascending order
- Print all permutation in lexicographic order
- Print a list in reverse order with range?
- Print binary tree in BFS fashion with O1 space
- Print all unique combination of factors of a given number
- Print Specific nodes at a every level calculated by a given function

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.