Program to check the Spiral Traversal on a Matrix
Definition & Explanation
Example:
Input:
Output:
More Description
Program Logic
Desc
C
Method 1 :
#include <stdio.h>
void spiralTraversal(int matrix[3][3]) {
int top = 0, bottom = 2, left = 0, right = 2;
while (top <= bottom && left <= right) {
for (int col = left; col <= right; ++col)
printf("%d ", matrix[top][col]);
++top;
for (int row = top; row <= bottom; ++row)
printf("%d ", matrix[row][right]);
--right;
if (top <= bottom) {
for (int col = right; col >= left; --col)
printf("%d ", matrix[bottom][col]);
--bottom;
}
if (left <= right) {
for (int row = bottom; row >= top; --row)
printf("%d ", matrix[row][left]);
++left;
}
}
}
int main() {
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
spiralTraversal(matrix);
return 0;
}
Output :
C++
Method 1 :
#include <iostream>
#include <vector>
using namespace std;
vector<int> spiralTraversal(vector<vector<int>>& matrix) {
vector<int> result;
int rows = matrix.size();
if (rows == 0) return result;
int cols = matrix[0].size();
int top = 0, bottom = rows - 1, left = 0, right = cols - 1;
while (top <= bottom && left <= right) {
for (int col = left; col <= right; ++col)
result.push_back(matrix[top][col]);
++top;
for (int row = top; row <= bottom; ++row)
result.push_back(matrix[row][right]);
--right;
if (top <= bottom) {
for (int col = right; col >= left; --col)
result.push_back(matrix[bottom][col]);
--bottom;
}
if (left <= right) {
for (int row = bottom; row >= top; --row)
result.push_back(matrix[row][left]);
++left;
}
}
return result;
}
int main() {
vector<vector<int>> matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
vector<int> result = spiralTraversal(matrix);
for (int num : result)
cout << num << " ";
cout << endl;
return 0;
}
Output :
JAVA
Method 1 :
import java.util.ArrayList;
import java.util.List;
public class Main {
public static List<Integer> spiralTraversal(int[][] matrix) {
List<Integer> result = new ArrayList<>();
int rows = matrix.length;
if (rows == 0) return result;
int cols = matrix[0].length;
int top = 0, bottom = rows - 1, left = 0, right = cols - 1;
while (top <= bottom && left <= right) {
for (int col = left; col <= right; ++col)
result.add(matrix[top][col]);
++top;
for (int row = top; row <= bottom; ++row)
result.add(matrix[row][right]);
--right;
if (top <= bottom) {
for (int col = right; col >= left; --col)
result.add(matrix[bottom][col]);
--bottom;
}
if (left <= right) {
for (int row = bottom; row >= top; --row)
result.add(matrix[row][left]);
++left;
}
}
return result;
}
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
List<Integer> result = spiralTraversal(matrix);
for (int num : result)
System.out.print(num + " ");
System.out.println();
}
}
Output :
Python
Method 1 :
def spiral_traversal(matrix):
if not matrix:
return []
result = []
rows, cols = len(matrix), len(matrix[0])
top, bottom, left, right = 0, rows - 1, 0, cols - 1
while top <= bottom and left <= right:
# Traverse top row
for col in range(left, right + 1):
result.append(matrix[top][col])
top += 1
# Traverse right column
for row in range(top, bottom + 1):
result.append(matrix[row][right])
right -= 1
# Traverse bottom row (if applicable)
if top <= bottom:
for col in range(right, left - 1, -1):
result.append(matrix[bottom][col])
bottom -= 1
# Traverse left column (if applicable)
if left <= right:
for row in range(bottom, top - 1, -1):
result.append(matrix[row][left])
left += 1
return result
# Example usage:
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print(spiral_traversal(matrix))
Output :
[1, 2, 3, 6, 9, 8, 7, 4, 5]
[Finished in 339ms]