Skip to content

Spiral Matrix feature is added which was requested #4455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/SpiralMatrix/SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.thealgorithms.SpiralMatrix;

public class SpiralMatrix {
public static int[][] generateSpiralMatrix(int n) {
int[][] matrix = new int[n][n];
int num = 1;
int top = 0, bottom = n - 1, left = 0, right = n - 1;

while (num <= n * n) {
// Traverse from left to right along the top row
for (int i = left; i <= right; i++) {
matrix[top][i] = num++;
}
top++;

// Traverse from top to bottom along the right column
for (int i = top; i <= bottom; i++) {
matrix[i][right] = num++;
}
right--;

// Traverse from right to left along the bottom row
for (int i = right; i >= left; i--) {
matrix[bottom][i] = num++;
}
bottom--;

// Traverse from bottom to top along the left column
for (int i = bottom; i >= top; i--) {
matrix[i][left] = num++;
}
left++;
}

return matrix;
}

public static void main(String[] args) {
int n = 3; // Replace with the desired value of n
int[][] result = generateSpiralMatrix(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}