Spiraling the elements of a square matrix JavaScript


In this problem statement, our main aim is to spiral the elements of the square matrix with the help of Javascript functionalities. There are several methods that can be used to do this task in Javascript.

Understanding the problem statement

The problem statement is to write a function in Javascript that will show the output as spiraling the elements of a square matrix. For example, if we have a two dimensional matrix as [1,2] [3,4], if we spiral the elements in clockwise direction then the output array will be [1,2,4,3].

Logic for the given problem

For the code we will implement a function to do the given work. Inside this function we will pass a parameter of matrix to which we need to make a spiral array. This function will convert the passed matrix in the clockwise spiral order array. We will do this process by maintaining four variables. These variables will represent the boundaries of the matrix first rowStart, second rowEnd, third colStart and colEnd. Inside this function we will iterate the elements of the matrix and push the elements into the result array.

Algorithm

Step 1 − Declare a function called spiralElements which is using a parameter of matrix.

Step 2 − Define required variables. A result array in which we will store the resultant array. Then starting and ending points of row and column and named them rowStart, rowEnd, colStart and colEnd.

Step 3 − Loop through using a while loop and run this loop until the starting point is less than the ending point.

Step 4 − And inside this loop we will traverse these elements in right, left, up and down directions one by one.

Step 5 − Return the result of the spiral array of the given matrix.

Code for the algorithm

//function to spiraling the matrix elements
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;
}
const matrix = [ [7, 8, 9], [4, 5, 6], [1, 2, 3] ];
console.log(spiralElements(matrix));

In the above output we can see the spiraling elements of the given square matrix. The spiral is in a clockwise direction. If we start making spirals from 7 (the first element), after that we will travers 8, 9, then 6 (second-row third column), 3 (third-row third column), 2, 1, 4 , 5.

Complexity

The time taken by the function is O(n^2) where n is the length of one side of the given square matrix. This is because we are traversing every element in the matrix exactly once. And the space used by the code is also O(n^2) as it is storing the result as a new array of spiral order of the elements.

Conclusion

So the above-mentioned function can be used to spiral the elements of the matrix in clockwise direction with the time complexity of O(n^2). We have used mainly four variables to keep track of the elements of the matrix to make a spiral in clockwise order.

Updated on: 18-May-2023

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements