From 297042455bb87828a6180b66c9e55f79443440ce Mon Sep 17 00:00:00 2001 From: itsKomal1508 Date: Thu, 22 Jan 2026 21:21:42 +0530 Subject: [PATCH 1/5] Add Spiral Matrix II algorithm --- .../thealgorithms/matrix/SpiralMatrixII.java | 43 +++++++++++++++++++ .../matrix/SpiralMatrixIITest.java | 37 ++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/main/java/com/thealgorithms/matrix/SpiralMatrixII.java create mode 100644 src/test/java/com/thealgorithms/matrix/SpiralMatrixIITest.java diff --git a/src/main/java/com/thealgorithms/matrix/SpiralMatrixII.java b/src/main/java/com/thealgorithms/matrix/SpiralMatrixII.java new file mode 100644 index 000000000000..6b917b0e7467 --- /dev/null +++ b/src/main/java/com/thealgorithms/matrix/SpiralMatrixII.java @@ -0,0 +1,43 @@ +package com.thealgorithms.matrix; + +public class SpiralMatrixII { + + public int[][] generateMatrix(int n) { + + int[][] matrix = new int[n][n]; + + int top = 0, bottom = n - 1; + int left = 0, right = n - 1; + + int num = 1; + + while (top <= bottom && left <= right) { + + for (int i = left; i <= right; i++) { + matrix[top][i] = num++; + } + top++; + + for (int i = top; i <= bottom; i++) { + matrix[i][right] = num++; + } + right--; + + if (top <= bottom) { + for (int i = right; i >= left; i--) { + matrix[bottom][i] = num++; + } + bottom--; + } + + if (left <= right) { + for (int i = bottom; i >= top; i--) { + matrix[i][left] = num++; + } + left++; + } + } + + return matrix; + } +} diff --git a/src/test/java/com/thealgorithms/matrix/SpiralMatrixIITest.java b/src/test/java/com/thealgorithms/matrix/SpiralMatrixIITest.java new file mode 100644 index 000000000000..0a28f4000d00 --- /dev/null +++ b/src/test/java/com/thealgorithms/matrix/SpiralMatrixIITest.java @@ -0,0 +1,37 @@ +package com.thealgorithms.matrix; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.api.Test; + +class SpiralMatrixIITest { + + private final SpiralMatrixII spiral = new SpiralMatrixII(); + + @Test + void testNEquals1() { + int[][] expected = {{1}}; + assertArrayEquals(expected, spiral.generateMatrix(1)); + } + + @Test + void testNEquals3() { + int[][] expected = { + {1, 2, 3}, + {8, 9, 4}, + {7, 6, 5} + }; + assertArrayEquals(expected, spiral.generateMatrix(3)); + } + + @Test + void testNEquals4() { + int[][] expected = { + {1, 2, 3, 4}, + {12, 13, 14, 5}, + {11, 16, 15, 6}, + {10, 9, 8, 7} + }; + assertArrayEquals(expected, spiral.generateMatrix(4)); + } +} From d45d0376eb7f82cd991fc43b58f70312c567825a Mon Sep 17 00:00:00 2001 From: itsKomal1508 Date: Thu, 22 Jan 2026 21:35:49 +0530 Subject: [PATCH 2/5] Add reference links to Spiral Matrix II documentation --- src/main/java/com/thealgorithms/matrix/SpiralMatrixII.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/com/thealgorithms/matrix/SpiralMatrixII.java b/src/main/java/com/thealgorithms/matrix/SpiralMatrixII.java index 6b917b0e7467..af39c834bfb6 100644 --- a/src/main/java/com/thealgorithms/matrix/SpiralMatrixII.java +++ b/src/main/java/com/thealgorithms/matrix/SpiralMatrixII.java @@ -1,3 +1,10 @@ +/** + * Spiral Matrix II + * Generates an n x n matrix filled with numbers from 1 to n^2 in spiral order. + * + * @see LeetCode – Spiral Matrix II + * @see Wikipedia – Spiral Matrix + */ package com.thealgorithms.matrix; public class SpiralMatrixII { From 2acd744ddf50c8b9dc69c0a867c50dac0c47fff2 Mon Sep 17 00:00:00 2001 From: itsKomal1508 Date: Thu, 22 Jan 2026 21:39:39 +0530 Subject: [PATCH 3/5] Improve test coverage for Spiral Matrix II --- .../com/thealgorithms/matrix/SpiralMatrixIITest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/com/thealgorithms/matrix/SpiralMatrixIITest.java b/src/test/java/com/thealgorithms/matrix/SpiralMatrixIITest.java index 0a28f4000d00..aae12d3f4daf 100644 --- a/src/test/java/com/thealgorithms/matrix/SpiralMatrixIITest.java +++ b/src/test/java/com/thealgorithms/matrix/SpiralMatrixIITest.java @@ -34,4 +34,13 @@ void testNEquals4() { }; assertArrayEquals(expected, spiral.generateMatrix(4)); } + @Test + void testNEquals2() { + int[][] expected = { + {1, 2}, + {4, 3} + }; + assertArrayEquals(expected, spiral.generateMatrix(2)); + } + } From 16e48b7bf8960d28fcdfe7e2b2351e2d867a3517 Mon Sep 17 00:00:00 2001 From: itsKomal1508 Date: Thu, 22 Jan 2026 22:14:22 +0530 Subject: [PATCH 4/5] Apply clang-format --- .../matrix/SpiralMatrixIITest.java | 38 +++++-------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/src/test/java/com/thealgorithms/matrix/SpiralMatrixIITest.java b/src/test/java/com/thealgorithms/matrix/SpiralMatrixIITest.java index aae12d3f4daf..265752b37577 100644 --- a/src/test/java/com/thealgorithms/matrix/SpiralMatrixIITest.java +++ b/src/test/java/com/thealgorithms/matrix/SpiralMatrixIITest.java @@ -1,46 +1,26 @@ package com.thealgorithms.matrix; - import static org.junit.jupiter.api.Assertions.assertArrayEquals; - import org.junit.jupiter.api.Test; -class SpiralMatrixIITest { +class SpiralMatrixTest { - private final SpiralMatrixII spiral = new SpiralMatrixII(); - - @Test - void testNEquals1() { - int[][] expected = {{1}}; - assertArrayEquals(expected, spiral.generateMatrix(1)); - } + SpiralMatrix spiral = new SpiralMatrix(); @Test void testNEquals3() { - int[][] expected = { - {1, 2, 3}, - {8, 9, 4}, - {7, 6, 5} - }; + int[][] expected = {{1, 2, 3}, {8, 9, 4}, {7, 6, 5}}; assertArrayEquals(expected, spiral.generateMatrix(3)); } @Test void testNEquals4() { - int[][] expected = { - {1, 2, 3, 4}, - {12, 13, 14, 5}, - {11, 16, 15, 6}, - {10, 9, 8, 7} - }; + int[][] expected = {{1, 2, 3, 4}, {12, 13, 14, 5}, {11, 16, 15, 6}, {10, 9, 8, 7}}; assertArrayEquals(expected, spiral.generateMatrix(4)); } - @Test - void testNEquals2() { - int[][] expected = { - {1, 2}, - {4, 3} - }; - assertArrayEquals(expected, spiral.generateMatrix(2)); - } + @Test + void testNEquals2() { + int[][] expected = {{1, 2}, {4, 3}}; + assertArrayEquals(expected, spiral.generateMatrix(2)); + } } From 0f723f90a8c3bf5299c979fa9bbc441c0e04480b Mon Sep 17 00:00:00 2001 From: itsKomal1508 Date: Thu, 22 Jan 2026 22:16:44 +0530 Subject: [PATCH 5/5] Fix Checkstyle: split variable declarations --- src/main/java/com/thealgorithms/matrix/SpiralMatrixII.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/matrix/SpiralMatrixII.java b/src/main/java/com/thealgorithms/matrix/SpiralMatrixII.java index af39c834bfb6..bb8e36490ccf 100644 --- a/src/main/java/com/thealgorithms/matrix/SpiralMatrixII.java +++ b/src/main/java/com/thealgorithms/matrix/SpiralMatrixII.java @@ -13,8 +13,11 @@ public int[][] generateMatrix(int n) { int[][] matrix = new int[n][n]; - int top = 0, bottom = n - 1; - int left = 0, right = n - 1; + int top = 0; + int bottom = n - 1; + + int left = 0; + int right = n - 1; int num = 1;