From 26650c72e1d92685cd307afc119380646672447c Mon Sep 17 00:00:00 2001 From: Manisha Rana Date: Tue, 6 Jan 2026 18:55:03 -0500 Subject: [PATCH] Array-1 Done --- DiagonalTraverse.java | 59 ++++++++++++++++++++++++++++++++++++++++ ProductExceptSelf.java | 30 +++++++++++++++++++++ SpiralMatrix.java | 61 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 DiagonalTraverse.java create mode 100644 ProductExceptSelf.java create mode 100644 SpiralMatrix.java diff --git a/DiagonalTraverse.java b/DiagonalTraverse.java new file mode 100644 index 00000000..ed5720ce --- /dev/null +++ b/DiagonalTraverse.java @@ -0,0 +1,59 @@ +// Time Complexity : O(MXN), Go over each element of the matrix once +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Approach : +// We need to identify whether the diagonal is going in upward direction or downward direction. To identify that, +// we can increment it, if the diagonal is even, we move downwards and increment row by 1 and decrement col by 1. +// If the digonal is odd, we move upwards, increment col by 1 and decrement row by 1; +// We will have to put additional checks when row or col becomes 0 or size, to avoid index of bounds error. +public class DiagonalTraverse { + + public int[] findDiagonalOrder(int[][] matrix) { + + int m = matrix.length; + if(m == 0) return new int[0]; + int n = matrix[0].length; + if(n == 0) return new int[0]; + int total = m * n; + int[] result = new int[total]; + + int diagonal = 1; + int index = 0; + int row = 0, col = 0; + while (index < total) { + result[index] = matrix[row][col]; + index++; + if (diagonal % 2 == 0) { + // To handle scenario where the cell is in both first col and last row + // Dont go out of bounds when in last row + if (col == 0 && row != m - 1) { + row++; + diagonal++; + } else if (row == m - 1) { + col++; + diagonal++; + } else { + row++; + col--; + } + } else { + // To handle scenario where the cell is in both first row and last col + // Dont go out of bounds when in last col + if (row == 0 && col != n - 1) { + col++; + diagonal++; + } else if (col == n - 1) { + row++; + diagonal++; + } else { + row--; + col++; + } + + } + } + + return result; + } + +} diff --git a/ProductExceptSelf.java b/ProductExceptSelf.java new file mode 100644 index 00000000..01b08e3d --- /dev/null +++ b/ProductExceptSelf.java @@ -0,0 +1,30 @@ +// Time Complexity : O(N), go through all elements to find the product +// Space Complexity : O(1), no additional space used +// Did this code successfully run on Leetcode : Yes +// Approach : + +// We can keep prefix product at index i without including value at index i. So in the first pass, we can multiply values +// from left to right, where the product at [i] is of values at [i-1]. Similarly, we multiply values from right to left +// and multiply it with exiting product at [i]; +public class ProductExceptSelf { + + public int[] productExceptSelf(int[] nums) { + int size = nums.length; + int[] product = new int[size]; + + int temp = 1; + product[0] = temp; + for(int i=1; i < size; i++){ + product[i] = temp * nums[i-1]; + temp *= nums[i-1]; + } + + temp = 1; + for(int i= size-2; i >= 0; i--){ + product[i] = product[i] * temp * nums[i+1]; + temp *= nums[i+1]; + } + + return product; + } +} diff --git a/SpiralMatrix.java b/SpiralMatrix.java new file mode 100644 index 00000000..c029008a --- /dev/null +++ b/SpiralMatrix.java @@ -0,0 +1,61 @@ +// Time Complexity : O(MXN), Go over each element of the matrix once +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Approach : +// To traverse in spiral order, we need to move from left to right direction, then top to bottom, them right to left and +// finally bottom to up. So we will use 4 pointers top, bottom, left and right. Since the spiral would be shrinking at +// every level, we need to shrink our boundaries too, thus reduce our pointers by 1 each time. +import java.util.ArrayList; +import java.util.List; + +class SpiralMatrix { + public List spiralOrder(int[][] matrix) { + int m = matrix.length; + int n = matrix[0].length; + List result = new ArrayList<>(); + if (m == 1 && n == 1) { + result.add(matrix[0][0]); + return result; + } + + int top = 0, left = 0; + int bottom = m - 1, right = n - 1; + + //Keep adding elements till the boundaries cross each other + // top == bottom or left == right is valid case + while (top <= bottom && left <= right) { + + for (int i = left; i <= right; i++) { + result.add(matrix[top][i]); + } + top++; + // check boundaries since pointer was updated in last line + if (isInvalidBounds(top, bottom)) break; + + for (int i = top; i <= bottom; i++) { + result.add(matrix[i][right]); + } + right--; + + if (isInvalidBounds(left, right)) break; + + for (int i = right; i >= left; i--) { + result.add(matrix[bottom][i]); + } + bottom--; + + if (isInvalidBounds(top, bottom)) break; + + for (int i = bottom; i >= top; i--) { + result.add(matrix[i][left]); + } + left++; + } + + return result; + } + + public boolean isInvalidBounds(int start, int end) { + return start > end; + } +} \ No newline at end of file