How to use Boundary Traversal Approach In Javascript
To print a given matrix in a spiral form we initialize boundaries to track the matrix’s edges. Then iterate through the matrix, printing elements in a clockwise spiral pattern: top row, right column, bottom row, left column. Adjust boundaries after each step and continue until all elements are printed.
- Initialize four variables to represent the boundaries of the matrix:
- top: The topmost row.
- bottom: The bottommost row.
- left: The leftmost column.
- right: The rightmost column.
- Iterate over all the elements of the matrix:
- Print all elements of the top row from left to right and increment the top pointer to move to the next row.
- Now, print all elements of the right column from top to bottom and decrement the right pointer to the previous column.
- Check that, if the top pointer is still less than or equal to the bottom boundary.
- If yes, then print all elements of the bottom row from right to left and decrement the bottom to its previous row.
- Again check if the left pointer is still less than or equal to the right boundary.If yes, then print all elements of the left column from bottom to top and increment the left to its next column.
- Repeat the above steps until all elements are printed.
Example: Below is the implementation of the above approach:
Javascript
// JavaScript code for the above approach function printSpiral(matrix) { let result = []; let top = 0; let bottom = matrix.length - 1; let left = 0; let right = matrix[0].length - 1; // Loop while the elements // are within the boundaries. while (top <= bottom && left <= right) { // Print top row for (let i = left; i <= right; i++) { result.push(matrix[top][i]); } // Move the top boundary down. top++; // Print right column for (let i = top; i <= bottom; i++) { result.push(matrix[i][right]); } // Move the right boundary to the left. right--; // Check if there are more rows if (top <= bottom) { // Print bottom row for (let i = right; i >= left; i--) { result.push(matrix[bottom][i]); } // Move the bottom boundary up. bottom--; } // Check if there are more columns if (left <= right) { // Print left column for (let i = bottom; i >= top; i--) { result.push(matrix[i][left]); } // Move the left boundary to the right. left++; } } // Print the result console.log(result.join( ' ' )); } // Example const matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16] ]; printSpiral(matrix); |
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Time Complexity: O(m * n): The time complexity of this code is O(m * n), where ‘m’ is the number of rows in the matrix, and ‘n’ is the number of columns in the matrix. This is because we visit each element in the matrix exactly once in a linear fashion.
Space Complexity: O(m * n): The space complexity is O(m * n) as well. The primary space consumption comes from the result array, which stores all the elements of the matrix in spiral order. The size of this array is directly proportional to the number of elements in the matrix, which is ‘m * n’.
JavaScript Program to Print Given Matrix in Spiral Form
Write a JavaScript program to print a given 2D matrix in spiral form.
You are given a two-dimensional matrix of integers. Write a program to traverse the matrix starting from the top-left corner which moves right, bottom, left, and up in a spiral pattern until all the elements are visited. Let us understand it with the examples and figures given below:
Examples:
Example 1:
Input: matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Output: [1 2 3 6 9 8 7 4 5]
Example 2:
Input: matrix = [[1, 2, 3, 4],
[12, 13, 14, 5],
[11, 16, 15, 6],
[10, 9, 8, 7]]
Output: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
Table of Content
- Using Boundary Traversal Approach
- Using Simulation Approach
- Using Recursion in JavaScript
- Using Depth First Search (DFS)
Contact Us