Print Matrix in Spiral Form

Posted in

Print Matrix in Spiral Form
maazbinasad

Maaz Bin Asad
Last updated on March 19, 2024

    Problem

    Given an integer matrix, print it in spiral form

    Sample Input

    [1 2 3 4]
    [4 5 6 7]
    [8 9 10 11]

    Sample Output

    1 2 3 4 7 11 10 9 8 4 5 6

    Approach

    We can maintain four variables and four for loops to traverse four edges of the matrix. The edges keep on shrinking after every iteration around the matrix. The time complexity of the approach is O(MxN).

    C++

    #include <iostream>
    #include <vector>
    usingnamespacestd;
    voidsolve(vector<vector<int>> matrix, int top, int bottom, int left, int right)
    {
     if (matrix.size() == 0 || left > right) {
     return;
     }
     // print top row
     for (int i = left; i <= right; i++) {
     cout << matrix[top][i] << " ";
     }
     top++;
     if (top > bottom) {
     return;
     }
     // print right column
     for (int i = top; i <= bottom; i++) {
     cout << matrix[i][right] << " ";
     }
     right--;
     if (left > right) {
     return;
     }
     // print bottom row
     for (int i = right; i >= left; i--) {
     cout << matrix[bottom][i] << " ";
     }
     bottom--;
     if (top > bottom) {
     return;
     }
     // print left column
     for (int i = bottom; i >= top; i--) {
     cout << matrix[i][left] << " ";
     }
     left++;
     solve(matrix, top, bottom, left, right);
    }
    intmain()
    {
     vector<vector<int>> matrix =
     {
     { 1, 2, 3, 4, 5},
     {16, 17, 18, 19, 6},
     {15, 24, 25, 20, 7}
     };
     int top = 0, bottom = matrix.size() - 1;
     int left = 0, right = matrix[0].size() - 1;
     solve(matrix, top, bottom, left, right);
     return0;
    }

    Output

    1 2 3 4 5 6 7 20 25 24 15 16 17 18 19

    C

    #include<stdio.h>
    voidsolve(int matrix[3][5], int top, int bottom, int left, int right)
    {
     if (left > right) {
     return;
     }
     // print top row
     for (int i = left; i <= right; i++) {
     printf("%d ",matrix[top][i]);
     }
     top++;
     if (top > bottom) {
     return;
     }
     // print right column
     for (int i = top; i <= bottom; i++) {
     printf("%d ",matrix[i][right]);
     }
     right--;
     if (left > right) {
     return;
     }
     // print bottom row
     for (int i = right; i >= left; i--) {
     printf("%d ",matrix[bottom][i]);
     }
     bottom--;
     if (top > bottom) {
     return;
     }
     // print left column
     for (int i = bottom; i >= top; i--) {
     printf("%d ",matrix[i][left]);
     }
     left++;
     solve(matrix, top, bottom, left, right);
    }
    intmain()
    {
     int matrix[3][5] =
     {
     { 1, 2, 3, 4, 5},
     {16, 17, 18, 19, 6},
     {15, 24, 25, 20, 7}
     };
     int top = 0, bottom = 2;
     int left = 0, right = 4;
     solve(matrix, top, bottom, left, right);
     return0;
    }
    

    Output

    1 2 3 4 5 6 7 20 25 24 15 16 17 18 19

    Python

    defsolve(matrix, top, bottom, left, right):
     if left > right:
     return
     # print top row
     for i in range(left, right + 1):
     print(matrix[top][i], end=' ')
     top = top + 1
     if top > bottom:
     return
     # print right column
     for i in range(top, bottom + 1):
     print(matrix[i][right], end=' ')
     right = right - 1
     if left > right:
     return
     # print bottom row
     for i in range(right, left - 1, -1):
     print(matrix[bottom][i], end=' ')
     bottom = bottom - 1
     if top > bottom:
     return
     # print left column
     for i in range(bottom, top - 1, -1):
     print(matrix[i][left], end=' ')
     left = left + 1
     solve(matrix, top, bottom, left, right)
    matrix = [
     [1, 2, 3, 4, 5],
     [16, 17, 18, 19, 6],
     [15, 24, 25, 20, 7]
    ]
    top = 0
    bottom = len(matrix) - 1
    left = 0
    right = len(matrix[0]) - 1
    solve(matrix, top, bottom, left, right)
    

    Output

    1 2 3 4 5 6 7 20 25 24 15 16 17 18 19

    People are also reading:

    Leave a Comment on this Post

    0 Comments