Spiral Matrix

Easy
0/40
5 upvotes
Asked in companies
CultfitGoldman SachsDunzo

Problem statement

You are given a N x M matrix of integers, print the spiral path of the matrix.

For example:

Spiral Path

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first line of each test case contains two single space-separated integers N and M, denoting the number of rows and columns respectively.

The next 'N' lines, each contains 'M' single space-separated integers representing the elements in a row of the matrix.
Output format :
For each test case/query, print the spiral path of the given matrix.

Output for every test case will be printed in a separate line.
Note:
 You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 100
1 <= M <= 100
-10^9 <= data <= 10^9

where ‘T’ is the number of test cases,  ‘N’ and ‘M’ are the numbers of rows and columns respectively and ‘data’ is the value of the elements of the matrix.
Sample Input 1 :
3
3 6
1 2 3 4 5 6 
7 8 9 10 11 12 
13 14 15 16 17 18
1 1
4
1 5
1 2 3 4 5
Sample Output 1 :
1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11
4
1 2 3 4 5
Explanation of the Sample Input 1 :
The spiral path for test case 1 is as shown below:

Test Case 1 In the second test case, there is only one element in the matrix, so the spiral path is only that element. In the third test case, there is only one row or 1-D matrix, so the spiral path is only the single traversal of the matrix.

Sample Input 2 :
1
4 4
1 2 3 4 
5 6 7 8 
9 10 11 12 
13 14 15 16
Sample Output 2 :
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Hint

Print the boundary of the Matrix recursively.

Approaches (3)
Recursive

For each boundary, we will use the 4 loops. Now the problem is reduced to print the remaining submatrix which is handled with the help of recursion. Keep track of new dimensions(rows and columns) of the current submatrix which are passed with the help of parameters in each recursion call.
 

Algorithm:
 

spiralPrint(matrix[][], startingRow, startingCol, endingRow, endingCol):

  • Check for base cases, i.e. if outside the boundary of the matrix, then return.
  • Print the first row from left to right of the matrix i.e from column ‘startingCol’ to ‘endingCol’, print the elements of the ‘startingRow’ row.
  • Print the last column from top to bottom of the matrix i.e from row ‘startingRow+1’ to ‘endingRow’, print the elements of the ‘endingCol’ column.
  • Print the last row from right to left of the matrix i.e from column ‘endingCol-1’ to ‘startingCol’, print the elements of the ‘endingRow’ row.
  • Print the first column from bottom to top of the matrix i.e from row ‘endingRow-1’ to ‘startingRow+1’, print the elements of the Cth column.
  • Call the function recursively with the updated values of boundaries, spiralPrint(matrix, startingRow+1, startingCol+1, endingRow-1, endingCol-1)
Time Complexity

O(N * M), where N is the number of rows and M is the number of columns of the matrix.
 

We are considering each element of the matrix only once. Thus, the final time complexity is O(N * M).

Space Complexity

O(N * M), where N is the number of rows and M is the number of columns of the matrix.
 

In the worst case, there can be a maximum of row or columns number of recursive calls at a time in the stack. So, O(max(N, M)) extra space will be required by the recursive stack. We are also making an array to store the spiral path which will take O(N * M) extra space. Thus, the final space complexity is O(N * M + max(N, M)) ~ O(N * M).

Code Solution
(100% EXP penalty)
Spiral Matrix
Full screen
Console