From 5d14ca62d4d6029cb9b271a4b5e5355bd272f8e1 Mon Sep 17 00:00:00 2001 From: Tharun Balaji R <95350584+TharunBalaji2004@users.noreply.github.com> Date: Sun, 29 Jan 2023 15:29:23 +0530 Subject: [PATCH 1/2] Delete SpiralMatrix.java --- 4_2DArrays/SpiralMatrix.java | 51 ------------------------------------ 1 file changed, 51 deletions(-) delete mode 100644 4_2DArrays/SpiralMatrix.java diff --git a/4_2DArrays/SpiralMatrix.java b/4_2DArrays/SpiralMatrix.java deleted file mode 100644 index a3affad..0000000 --- a/4_2DArrays/SpiralMatrix.java +++ /dev/null @@ -1,51 +0,0 @@ -import java.util.*; - -//Problem : Print a Matrix in Spiral Fashion - -public class SpiralMatrix { - public static void printSpiral(int matrix[][]) { - int startRow = 0; - int startCol = 0; - int endRow = matrix.length-1; - int endCol = matrix[0].length-1; - - //print boundaries - while(startRow <= endCol && startCol <= endCol) { - for(int j=startCol; j<=endCol; j++) { - System.out.print(matrix[startRow][j]+" "); - } - - for(int i=startRow+1; i<=endRow; i++) { - System.out.print(matrix[i][endCol]+" "); - } - - for(int j=endCol-1; j>=startCol; j--) { - if(startRow == endRow) { - break; - } - System.out.print(matrix[endRow][j]+" "); - } - - for(int i=endRow-1; i>startRow; i--) { - if(startCol == endCol) { - break; - } - System.out.print(matrix[i][startCol]+" "); - } - - startCol++; startRow++; - endCol--; endRow--; - } - - System.out.println(); - } - - public static void main(String args[]) { - int matrix[][] = {{1, 2, 3, 4}, - {5, 6, 7, 8}, - {9, 10, 11, 12}, - {13, 14, 15, 16}}; - printSpiral(matrix); - } -} - \ No newline at end of file From 725c3e96188adeb2b7a2754a5281fb32d6356dc0 Mon Sep 17 00:00:00 2001 From: Tharun Balaji R <95350584+TharunBalaji2004@users.noreply.github.com> Date: Sun, 29 Jan 2023 15:31:28 +0530 Subject: [PATCH 2/2] Updated Spiral Matrix Code --- 4_2DArrays/SpiralMatrix.java | 48 ++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 4_2DArrays/SpiralMatrix.java diff --git a/4_2DArrays/SpiralMatrix.java b/4_2DArrays/SpiralMatrix.java new file mode 100644 index 0000000..bd07e9f --- /dev/null +++ b/4_2DArrays/SpiralMatrix.java @@ -0,0 +1,48 @@ +import java.util.*; + +public class SpiralMatrix { + public static List printSpiral(int[][] matrix) { + int startRow = 0; + int startCol = 0; + int endRow = matrix.length-1; + int endCol = matrix[0].length-1; + List list = new ArrayList(); + + while(startCol <= endRow && startRow <= endCol){ + for(int i=startRow; i<=endCol; i++) + list.add(matrix[startCol][i]); + startCol++; + + for(int i=startCol; i<=endRow; i++) + list.add(matrix[i][endCol]); + endCol--; + + if (startCol <= endRow){ + for(int i=endCol; i>=startRow; i--) + list.add(matrix[endRow][i]); + endRow--; + } + + if (startRow <= endCol){ + for(int i=endRow; i>=startCol; i--) + list.add(matrix[i][startRow]); + startRow++; + } + } + + return list; + } + + public static void main(String[] args) { + int[][] matrix1 = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}; + System.out.println(printSpiral(matrix1)); + int[][] matrix2 = {{1,2,3},{4,5,6},{7,8,9}}; + System.out.println(printSpiral(matrix2)); + } +} + +/* +OUTPUT: +1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 +1 2 3 6 9 8 7 4 5 +*/ \ No newline at end of file