From 566ffca99f75bbb276b77800179003c514030695 Mon Sep 17 00:00:00 2001 From: Snehal Yeole Date: Fri, 28 Nov 2025 23:42:02 -0800 Subject: [PATCH 1/2] Product of Array Except Self --- Array_Product_Except_Self.java | 42 ++++++++++++++++++++++++++++++++++ Sample.java | 6 ----- 2 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 Array_Product_Except_Self.java delete mode 100644 Sample.java diff --git a/Array_Product_Except_Self.java b/Array_Product_Except_Self.java new file mode 100644 index 00000000..047d36dd --- /dev/null +++ b/Array_Product_Except_Self.java @@ -0,0 +1,42 @@ +// Time Complexity : O(N) -> To traverse N elements of input array +// Space Complexity : O(1) -> Since the ouput array that we created is used to store left product of each input element and then update each left product in output array with right product * left product and finally return as output. So, no extra space used +// Did this code successfully run on Leetcode : Yes +// We create an output array to store the left product of each element in the input array. Then we traverse the input array from the end to calculate the right product and update the output array by multiplying the right product with the left product already stored in the output array. + +// Your code here along with comments explaining your approach +class Solution { + public int[] productExceptSelf(int[] nums) { + // Base condition - If array is null, no product + if(nums == null || nums.length == 0){ + return new int[0]; + } + + // Create a new output array + // In that output array, we will store left product of each element first + // Then, calculate right product of each element and update output array with left*right product + int[] output = new int[nums.length]; + // Initialize a product to 1 - left product of 1st element in input array will be 1 + int product = 1; + + /////LEFT PRODUCT////// + // Calculate left product of each element in input array and store in output array + for(int i=0; i < nums.length; i++){ + output[i] = product; // For element at index i, store the left product in output array + // calculate and update left product for next element + product = product * nums[i]; + } + + /////RIGHT PRODUCT////// + // Calculate right product of each element and update output array with left*right product. start at last element of input array to calculate right product + // Reset product = 1 as right product of last element in input array will be 1 + product = 1; + for(int i = nums.length - 1; i >= 0; i--){ + // We have the right product for element i - We multiply right product with left product in output array at index i and update output array + //output[i] = left product * right product; + output[i] = output[i] * product; + // calculate and update right product for previous element + product = product * nums[i]; + } + return output; + } +} diff --git a/Sample.java b/Sample.java deleted file mode 100644 index f10cd8d1..00000000 --- a/Sample.java +++ /dev/null @@ -1,6 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Three line explanation of solution in plain english - -// Your code here along with comments explaining your approach \ No newline at end of file From 2e2006a8f257d718718f60c67dd50d6ccda8ce40 Mon Sep 17 00:00:00 2001 From: Snehal Yeole Date: Sun, 30 Nov 2025 02:37:53 -0800 Subject: [PATCH 2/2] Completed Array-1 Problems: Product Except Self, Diagonal Traverse, and Spiral Matrix --- Array_Product_Except_Self.java | 2 +- Diagonal_Traverse.java | 68 ++++++++++++++++++++++++++++++++++ Spiral_Matrix.java | 65 ++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 Diagonal_Traverse.java create mode 100644 Spiral_Matrix.java diff --git a/Array_Product_Except_Self.java b/Array_Product_Except_Self.java index 047d36dd..29699012 100644 --- a/Array_Product_Except_Self.java +++ b/Array_Product_Except_Self.java @@ -1,7 +1,7 @@ // Time Complexity : O(N) -> To traverse N elements of input array // Space Complexity : O(1) -> Since the ouput array that we created is used to store left product of each input element and then update each left product in output array with right product * left product and finally return as output. So, no extra space used // Did this code successfully run on Leetcode : Yes -// We create an output array to store the left product of each element in the input array. Then we traverse the input array from the end to calculate the right product and update the output array by multiplying the right product with the left product already stored in the output array. +// We created an output array to store the left product of each element in the input array. Then we traverse the input array from the end to calculate the right product and update the output array by multiplying the right product with the left product already stored in the output array. // Your code here along with comments explaining your approach class Solution { diff --git a/Diagonal_Traverse.java b/Diagonal_Traverse.java new file mode 100644 index 00000000..5e3d9d61 --- /dev/null +++ b/Diagonal_Traverse.java @@ -0,0 +1,68 @@ +// Time Complexity : O(m * n) -> To traverse 'm' rows and 'n' columns in the given matrix +// Space Complexity : O(1) -> An array created is returned as an output so no extra space used +// Did this code successfully run on Leetcode : Yes +// We traverse the given matrix in upward and downward diagonal direction based on the current position of row and column pointers. We use a direction pointer to decide whether to traverse in upward or downward direction. We fill in the output array with elements from the matrix as we traverse it. + +// Your code here along with comments explaining your approach +class Solution { + public int[] findDiagonalOrder(int[][] mat) { + // Base condition + if(mat == null || mat.length == 0 || mat[0].length == 0){ + return new int[0]; // return empty array + } + + // Number of rows and columns + int m = mat.length; // 'm' rows + int n = mat[0].length; // 'n' columns + + // Initialize pointers to traverse matrix of size m * n + int i = 0; + int j = 0; + int dirs = 1; // dirs pointer to decide direction to traverse in matrix + + // Initialize index pointer to fill in the output array with elements + int index = 0; + + // Create an output array - size is equal to number of elements in input array - m * n + int[] output = new int[m * n]; + + // Traverse the given matrix based on upward/downward direction and fill in output array until index < m * n (length of matrix) + while(index < m * n){ + output[index] = mat[i][j]; + index += 1; + // Traversing in upward direction + if(dirs == 1){ + // last column - upward to downward direction change, next row + if(j == n - 1){ + dirs -= 1; // direction change to downward + i++; + // 1st row - upward to downward direction change, next column + }else if(i == 0){ + dirs -= 1; // direction change to downward + j++; + // except 1st row and last column - tarversal to upward direction is done by decrementing row and incrementing column + }else{ + i--; + j++; + } + // Traversing in downward direction + }else{ + // last row - downward to upward direction change, next column + if(i == m - 1){ + dirs += 1; // direction change to upward + j++; + // 1st column - downward to upward direction change, next row + }else if(j == 0){ + dirs += 1; // direction change to upward + i++; + // except last row and 1st column - traversal to downward direction is done by incrementing row and decrementing column + }else{ + i++; + j--; + } + } + + } + return output; + } +} \ No newline at end of file diff --git a/Spiral_Matrix.java b/Spiral_Matrix.java new file mode 100644 index 00000000..dfa6acc3 --- /dev/null +++ b/Spiral_Matrix.java @@ -0,0 +1,65 @@ +// Time Complexity : O(m * n) -> To traverse m * n elements in given matrix +// Space Complexity : O(1) -> Constant space. The output array/list created is returned as output so not considered as extra space +// Did this code successfully run on Leetcode : Yes +// We are traversing the matrix in spiral order by maintaining four pointers - top, bottom, left and right. We traverse the matrix in four directions - left to right, top to bottom, right to left and bottom to top and update the pointers accordingly until all elements are traversed + +// Your code here along with comments explaining your approach +class Solution { + public List spiralOrder(int[][] matrix) { + // Base condition + if(matrix == null || matrix.length == 0 || matrix[0].length == 0){ + return new ArrayList<>(); + } + + // 1. Create output Array + List output = new ArrayList<>(); + + // 2. Length of rows and columns + int m = matrix.length; // rows + int n = matrix[0].length; // columns + + // 3. Initialize the pointers + int top = 0; int left = 0; + int bottom = m - 1; int right = n - 1; + + // 4. Traverse the matrix elements in spiral order and store in output list + while(top <= bottom && left <= right){ + // top row: left -> right + // No need to check for top, bottom or left, right bounds as we just checked. So no IF condition added here + for(int x = left; x <= right; x++){ + output.add(matrix[top][x]); + } + top++; + + // right column: top -> bottom + // No need for (top <= bottom) condition as its getting checked in for loop. No need for (left <= right) as no change in left, right pointers yet + for(int x = top; x <= bottom; x++){ + output.add(matrix[x][right]); + } + right--; + + // bottom row: right -> left + // We need (top <= bottom) condition here, as top pointer is changed above. No need for (left <= right) as its being checked in for loop + if(top <= bottom){ + for(int x = right; x >= left; x--){ + output.add(matrix[bottom][x]); + } + bottom--; + } + + // left column: bottom -> top + // We need (left <= right) condition here, as right pointer is changed above. No need for (top <= bottom) as its being checked in for loop + if(left <= right){ + for(int x = bottom; x >= top; x--){ + output.add(matrix[x][left]); + } + left++; + } + } + + // 5. Return the output array/list + return output; + + } + +} \ No newline at end of file