Two Matrix Spiral Print
Print values from two square matrices in a clockwise spiral pattern, alternating between matrices for each row/column.
Understand the Problem
Problem Statement
The program must accept two square matrices of size N*N. Then the values in the matrices must be printed spirally in clockwise direction in a single line. The values in a specific row or column of the first matrix must follow the values in the same row or column of the second matrix.
Constraints
- 1 <= N <= 30
- 1 <= Integer value in the matrix <= 1000
- Both matrices are square and of equal size
- Input values are positive integers
Examples
5
10 20 30 40 50
60 70 80 90 91
88 86 34 35 36
21 22 23 24 25
71 72 73 74 75
101 102 103 104 105
106 107 108 109 110
111 112 113 114 115
116 117 118 119 120
121 122 123 124 12510 20 30 40 50 101 102 103 104 105 91 36 25 75 110 115 120 125 74 73 72 71 124 123 122 121 21 88 60 116 111 106 70 80 90 107 108 109 35 24 114 119 23 22 118 117 86 112 34 113Starting from the top-left corner, we traverse the outermost layer clockwise: first row of both matrices, then right column of both matrices, then bottom row of both matrices, then left column of both matrices. Then we move to the inner 3x3 layer and repeat the same pattern.
4
78 51 30 23
83 53 7 32
40 62 4 77
100 83 19 21
100 37 45 13
2 100 97 72
10 91 91 32
61 98 87 678 51 30 23 100 37 45 13 32 77 21 72 32 6 19 83 100 87 98 61 40 83 10 2 53 7 100 97 4 91 62 91We traverse the outer 4x4 layer first, then the inner 2x2 layer. For each direction (top, right, bottom, left), we print values from both matrices at those positions before moving to the next direction.
Solution
#include <stdio.h>
void spiralPrint(int n, int arr1[n][n], int arr2[n][n]) {
int top = 0, bottom = n - 1;
int left = 0, right = n - 1;
int i;
while (top <= bottom && left <= right) {
// Print top row of both matrices
for (i = left; i <= right; i++) {
printf("%d ", arr1[top][i]);
}
for (i = left; i <= right; i++) {
printf("%d ", arr2[top][i]);
}
top++;
// Print right column of both matrices
for (i = top; i <= bottom; i++) {
printf("%d ", arr1[i][right]);
}
for (i = top; i <= bottom; i++) {
printf("%d ", arr2[i][right]);
}
right--;
// Print bottom row if there are rows remaining
if (top <= bottom) {
for (i = right; i >= left; i--) {
printf("%d ", arr1[bottom][i]);
}
for (i = right; i >= left; i--) {
printf("%d ", arr2[bottom][i]);
}
bottom--;
}
// Print left column if there are columns remaining
if (left <= right) {
for (i = bottom; i >= top; i--) {
printf("%d ", arr1[i][left]);
}
for (i = bottom; i >= top; i--) {
printf("%d ", arr2[i][left]);
}
left++;
}
}
}
int main() {
int n;
scanf("%d", &n);
int arr1[n][n], arr2[n][n];
// Read first matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &arr1[i][j]);
}
}
// Read second matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &arr2[i][j]);
}
}
spiralPrint(n, arr1, arr2);
return 0;
}The C solution implements a spiral traversal using four boundary variables: top, bottom, left, and right. The algorithm processes the matrix layer by layer:
- It prints the top row from left to right for both matrices
- It prints the right column from top to bottom for both matrices
- It prints the bottom row from right to left for both matrices (if there are rows remaining)
- It prints the left column from bottom to top for both matrices (if there are columns remaining)
After each direction is processed, the corresponding boundary is moved inward. This continues until all layers are processed.