From 73f494301af41a36a72ff06cfba02d4b8ba65baa Mon Sep 17 00:00:00 2001 From: Harshitha Thondalapally Date: Tue, 6 Jan 2026 19:02:28 -0500 Subject: [PATCH] Completed Array-1 --- src/ArrayProduct.java | 36 ++++++++++++++++++++++++ src/DiagonalMatrix.java | 62 +++++++++++++++++++++++++++++++++++++++++ src/SpiralMatrix.java | 60 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 src/ArrayProduct.java create mode 100644 src/DiagonalMatrix.java create mode 100644 src/SpiralMatrix.java diff --git a/src/ArrayProduct.java b/src/ArrayProduct.java new file mode 100644 index 00000000..198f7a26 --- /dev/null +++ b/src/ArrayProduct.java @@ -0,0 +1,36 @@ +/* +Problem - Product of Array except self +Approach - First, we do a left-to-right pass storing running products before each index. +Then, we do a right-to-left pass multiplying the existing results with right-side products. +This way, each element gets the product of all other elements without using division. +Time Complexity - O(n) +Space Complexity - O(1) + */ + +import java.util.Arrays; + +public class ArrayProduct { + public int[] productExceptSelf(int[] nums) { + int n = nums.length; + int [] result = new int[n]; + int rp = 1; // running product + result[0] = 1; + for (int i = 1; i < n; i++) { // left traversal + rp = rp * nums[i - 1]; + result[i] = rp; + } + rp = 1; + for (int i = n - 2; i >= 0; i--) { // right traversal + rp = rp * nums[i + 1]; + result[i] = result[i] * rp; + } + return result; + } + public static void main(String[] args) { + ArrayProduct ap = new ArrayProduct(); + int[] product1 = ap.productExceptSelf(new int[] {1,2,3,4}); + int[] product2 = ap.productExceptSelf(new int[] {-1,1,0,-3,3}); + System.out.println(Arrays.toString(product1)); + System.out.println(Arrays.toString(product2)); + } +} diff --git a/src/DiagonalMatrix.java b/src/DiagonalMatrix.java new file mode 100644 index 00000000..0b1c17b5 --- /dev/null +++ b/src/DiagonalMatrix.java @@ -0,0 +1,62 @@ +/* +Problem - Diagonal traverse, return array of elements in diagonal order +Approach - Start from the top-left and traverse the matrix diagonally. +Flip direction when hitting the matrix boundaries (top, bottom, left, right). +Keep updating the result array as you move in up-right or down-left directions. +Time Complexity - O(m*n) +Space Complexity - O(1) + */ + + +import java.util.Arrays; + +public class DiagonalMatrix { + public int[] findDiagonalOrder(int[][] mat) { + int m = mat.length, n = mat[0].length; + int [] res = new int[m*n]; + int r =0, c = 0; + boolean direction = true; + for(int i = 0; i < m*n; i++){ + res[i] = mat[r][c]; + if(direction){ // moving up + if(c == n - 1){ + r++; + direction = false; + } else if (r == 0) { + c++; + direction = false; + } + else { + r--; + c++; + } + } + else{ // move down + if(r == m -1){ + c++; + direction = true; + } + else if(c == 0){ + r++; + direction = true; + } + else{ + r++; + c--; + } + } + } + + return res; + } + public static void main(String[] args) { + int[][] mat = {{1,2,3},{4,5,6},{7,8,9}}; + DiagonalMatrix d = new DiagonalMatrix(); + int [] res = d.findDiagonalOrder(mat); + System.out.println(Arrays.toString(res)); + int[][] mat2 = {{1,2},{3,4}}; + int [] res2 = d.findDiagonalOrder(mat2); + System.out.println(Arrays.toString(res2)); + } + +} diff --git a/src/SpiralMatrix.java b/src/SpiralMatrix.java new file mode 100644 index 00000000..1a421c47 --- /dev/null +++ b/src/SpiralMatrix.java @@ -0,0 +1,60 @@ +/* +Problem - Spiral , return array of elements in Spral order +Approach - We use four boundaries: top, bottom, left, right to keep track of the spiral path. +•At each step, we traverse right, down, left, then up while shrinking the boundaries. +•We stop when the boundaries cross each other. + +Time Complexity - O(m*n) +Space Complexity - O(1) + */ + +import java.util.ArrayList; +import java.util.List; + +public class SpiralMatrix { + public List spiralOrder(int[][] matrix) { + List res = new ArrayList<>(); + int m = matrix.length; + int n = matrix[0].length; + int top = 0; + int left = 0; + int right = n - 1; + int bottom = m - 1; + while (left <= right && top <= bottom) { + // traversing left to right + for (int i = left; i <= right; i++) { + res.add(matrix[top][i]); + } + top++; + // traversing top to bottom + for (int i = top; i <= bottom; i++) { + res.add(matrix[i][right]); + } + right--; + // check if bottom is crossing top as top got mutated above and then traverse right to left + if(top <= bottom) { + for (int i = right; i >= left; i--) { + res.add(matrix[bottom][i]); + } + bottom--; + } + // check if right is crossing left as left got mutated above and then traverse bottom to top + if(left <= right) { + for (int i = bottom; i >= top; i--) { + res.add(matrix[left][i]); + } + left++; + } + } // continue until the while check fails + return res; + } + public static void main(String[] args) { + SpiralMatrix sp = new SpiralMatrix(); + int[][] mat = {{1,2,3},{4,5,6},{7,8,9}}; + List res = sp.spiralOrder(mat); + System.out.println(res); + int[][] mat2 = {{1,2,3,4},{5,6,7,8},{9,10,11,12}}; + List res2 = sp.spiralOrder(mat2); + System.out.println(res2); + } +}