Spiraling the elements of a square matrix JavaScript

In this problem statement, our main aim is to spiral the elements of a square matrix with the help of JavaScript functionalities. We'll traverse the matrix in a clockwise direction, starting from the top-left corner and moving in a spiral pattern.

Understanding the Problem

The problem is to write a function in JavaScript that converts a 2D matrix into a 1D array by traversing elements in a clockwise spiral pattern. For example, if we have a matrix:

[
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]

The spiral traversal in clockwise direction would be: [1, 2, 3, 6, 9, 8, 7, 4, 5]

1 2 3 4 5 6 7 8 9 Clockwise Spiral Traversal

Algorithm

Step 1 ? Define four boundary variables: rowStart, rowEnd, colStart, and colEnd to track matrix boundaries.

Step 2 ? Traverse the matrix in four directions: right, down, left, and up.

Step 3 ? After each direction traversal, update the corresponding boundary.

Step 4 ? Continue until all elements are visited.

Step 5 ? Return the spiral array.

Implementation

function spiralElements(matrix) {
   const result = [];
   let rowStart = 0;
   let rowEnd = matrix.length - 1;
   let colStart = 0;
   let colEnd = matrix[0].length - 1;

   while (rowStart <= rowEnd && colStart <= colEnd) {
      // Traverse right
      for (let i = colStart; i <= colEnd; i++) {
         result.push(matrix[rowStart][i]);
      }
      rowStart++;

      // Traverse down
      for (let i = rowStart; i <= rowEnd; i++) {
         result.push(matrix[i][colEnd]);
      }
      colEnd--;

      // Traverse left
      if (rowStart <= rowEnd) {
         for (let i = colEnd; i >= colStart; i--) {
            result.push(matrix[rowEnd][i]);
         }
         rowEnd--;
      }

      // Traverse up
      if (colStart <= colEnd) {
         for (let i = rowEnd; i >= rowStart; i--) {
            result.push(matrix[i][colStart]);
         }
         colStart++;
      }
   }
   return result;
}

// Test with different matrix sizes
const matrix3x3 = [
   [1, 2, 3],
   [4, 5, 6],
   [7, 8, 9]
];

const matrix2x2 = [
   [1, 2],
   [3, 4]
];

console.log("3x3 Matrix Spiral:", spiralElements(matrix3x3));
console.log("2x2 Matrix Spiral:", spiralElements(matrix2x2));
3x3 Matrix Spiral: [1, 2, 3, 6, 9, 8, 7, 4, 5]
2x2 Matrix Spiral: [1, 2, 4, 3]

How It Works

The algorithm maintains four boundaries and traverses the matrix in layers:

  1. Right traversal: Move from left to right along the top row
  2. Down traversal: Move from top to bottom along the right column
  3. Left traversal: Move from right to left along the bottom row
  4. Up traversal: Move from bottom to top along the left column

After each traversal, we shrink the corresponding boundary to move inward to the next layer.

Time and Space Complexity

Complexity Value Explanation
Time O(n²) Visit each element exactly once
Space O(n²) Store result array with all matrix elements

Conclusion

The spiral matrix traversal algorithm efficiently converts a 2D matrix into a 1D array using boundary tracking. This technique is commonly used in coding interviews and matrix manipulation problems.

Updated on: 2026-03-15T23:19:00+05:30

351 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements