2D array
spiral order
algorithm
data structures
matrix traversal

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.

Practice algorithms

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:

  1. Identify the Center: Determine the starting point, which is the center of the matrix.
  2. Initialize Spiral Directions: Use vectors to signify the current direction of travel—right, down, left, and up.
  3. Edge Handling: Keep track of edges as the spiral expands and switch directions when the edge is reached.
  4. Store and Visit: Ensure each element is visited once and stored in an ordered sequence.
  5. 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 O(n×m)O(n \times m) where nn is the number of rows and mm 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.