From 28b23aef754e6bae8427edc58a385ff49da24e0f Mon Sep 17 00:00:00 2001 From: Simranb10 Date: Sun, 31 May 2026 23:42:24 -0400 Subject: [PATCH] Array-1 solution --- DiagonalTraverse.java | 45 +++++++++++++++++++++++++++++++++++ ProductExceptSelf.java | 24 +++++++++++++++++++ SpiralOrder.java | 53 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 DiagonalTraverse.java create mode 100644 ProductExceptSelf.java create mode 100644 SpiralOrder.java diff --git a/DiagonalTraverse.java b/DiagonalTraverse.java new file mode 100644 index 00000000..3e1e549e --- /dev/null +++ b/DiagonalTraverse.java @@ -0,0 +1,45 @@ +//Time Complexity: O(m*n) +//Space Complexity: O(1) + +public class DiagonalTraverse { + public int[] findDiagonalOrder(int[][] mat) { + int m = mat.length; + int n = mat[0].length; + int[] result = new int[m * n]; + + int row = 0; + int col = 0; + + boolean dir = true; + + for (int index = 0; index < m * n; index++) { + result[index] = mat[row][col]; + if (dir) { + if (col == n - 1) { + row++; + dir = false; + } else if (row == 0) { + col++; + dir = false; + } else { + row--; + col++; + + } + } else { + if (row == m - 1) { + col++; + dir = true; + + } else if (col == 0) { + row++; + dir = true; + } else { + row++; + col--; + } + } + } + return result; + } +} diff --git a/ProductExceptSelf.java b/ProductExceptSelf.java new file mode 100644 index 00000000..f9e9a985 --- /dev/null +++ b/ProductExceptSelf.java @@ -0,0 +1,24 @@ +//Time Complexity: O(n) +//Space Complexity: O(1) + +public class ProductExceptSelf { + int[] result; + + public int[] productExceptSelf(int[] nums) { + int n = nums.length; + result = new int[n]; + result[0] = 1; + + for (int i = 1; i < n; i++) { + result[i] = nums[i - 1] * result[i - 1]; + } + + int product = 1; + for (int i = n - 2; i >= 0; i--) { + product = product * nums[i + 1]; + result[i] = product * result[i]; + + } + return result; + } +} diff --git a/SpiralOrder.java b/SpiralOrder.java new file mode 100644 index 00000000..0788572d --- /dev/null +++ b/SpiralOrder.java @@ -0,0 +1,53 @@ +//Time Complexity : O(m*n) +//Space Complexity : O(m+n) + + +import java.util.ArrayList; +import java.util.List; + +public class SpiralOrder { + public List spiralOrder(int[][] matrix) { + List li = new ArrayList<>(); + + // Directrions + int top = 0; + int left = 0; + int bottom = matrix.length-1; + int right = matrix[0].length-1; + + helper(matrix,top,bottom,left,right,li); + return li; + } + + private void helper(int[][] matrix, int top, int bottom, int left, int right, List li) { + //base case : while loop condition + if(left > right || top > bottom) return; + + for (int i=left; i <= right; i++) { + li.add(matrix[top][i]); + } + top++; + //top to bottom + if(top <= bottom) { + for (int i=top; i <= bottom; i++) { + li.add(matrix[i][right]); + } + } + right--; + //right to left + if(top <= bottom) { + for (int i=right; i >= left; i--) { + li.add(matrix[bottom][i]); + } + } + bottom--; + //bottom to top + if(left <= right) { + for (int i=bottom; i >= top; i--) { + li.add(matrix[i][left]); + } + } + left++; + helper(matrix,top,bottom,left,right,li); + } +}