From 610d64232c8accec12bd331bee0b1d72cfcdc94d Mon Sep 17 00:00:00 2001 From: Hriday-A Date: Tue, 21 Apr 2026 19:40:46 -0500 Subject: [PATCH] Completed Array-1 Assignment --- Product_except_self.java | 17 +++++++++ diagonal_traverse.java | 80 ++++++++++++++++++++++++++++++++++++++++ spiral_matrix.java | 39 ++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 Product_except_self.java create mode 100644 diagonal_traverse.java create mode 100644 spiral_matrix.java diff --git a/Product_except_self.java b/Product_except_self.java new file mode 100644 index 00000000..41a10bf1 --- /dev/null +++ b/Product_except_self.java @@ -0,0 +1,17 @@ +class Solution { + public int[] productExceptSelf(int[] nums) { + int[] res = new int[nums.length]; + res[0]=1; + int rp=1; + for(int i=1;i=0;i--){ + rp = rp * nums[i+1]; + res[i] = res[i] * rp; + } + return res; + } +} \ No newline at end of file diff --git a/diagonal_traverse.java b/diagonal_traverse.java new file mode 100644 index 00000000..225dfe0e --- /dev/null +++ b/diagonal_traverse.java @@ -0,0 +1,80 @@ +class Solution { + public int[] findDiagonalOrder(int[][] mat) { + int m = mat.length; // number of rows + int n = mat[0].length; // number of columns + + boolean dir = true; // direction flag: true = up-right, false = down-left + + int r = 0, c = 0; // starting position (top-left corner) + int[] res = new int[m * n]; // result array to store traversal + + for (int i = 0; i < m * n; i++) { + res[i] = mat[r][c]; // add current element + + if (dir) { // moving up-right ↗ + + if (c == n - 1) { // hit right boundary + r++; // move down + dir = false; // change direction + } + else if (r == 0) { // hit top boundary + c++; // move right + dir = false; // change direction + } + else { + r--; // move up + c++; // move right + } + + } else { // moving down-left ↙ + + if (r == m - 1) { // hit bottom boundary + c++; // move right + dir = true; // change direction + } + else if (c == 0) { // hit left boundary + r++; // move down + dir = true; // change direction + } + else { + r++; // move down + c--; // move left + } + } + } + + return res; // return final diagonal traversal + } +} + +/* +==================== QUICK SUMMARY ==================== + +Goal: +Traverse matrix diagonally in zig-zag order. + +Core Idea: +- Use a direction flag: + true → move up-right (↗) + false → move down-left (↙) + +Movement Rules: +1. While moving UP (↗): + - If at right boundary → go DOWN, change direction + - If at top boundary → go RIGHT, change direction + - Else → go (r--, c++) + +2. While moving DOWN (↙): + - If at bottom boundary → go RIGHT, change direction + - If at left boundary → go DOWN, change direction + - Else → go (r++, c--) + +Key Insight: +- Direction only changes when we hit a boundary. +- Otherwise, keep moving diagonally. + +Time Complexity: O(m * n) +Space Complexity: O(1) (excluding output array) + +====================================================== +*/ \ No newline at end of file diff --git a/spiral_matrix.java b/spiral_matrix.java new file mode 100644 index 00000000..482cdfdc --- /dev/null +++ b/spiral_matrix.java @@ -0,0 +1,39 @@ +class Solution { + public List spiralOrder(int[][] matrix) { + int m = matrix.length; + int n = matrix[0].length; + int top =0, bottom =m-1, left =0, right = n-1; + List result = new ArrayList<>(); + while(left<=right && top<=bottom){ + for(int i=left; i<=right;i++){ + result.add(matrix[top][i]); + } + top++; + for(int i=top; i<=bottom;i++){ + result.add(matrix[i][right]); + } + right--; + if(top<=bottom){ + for(int i=right; i>=left;i--){ + result.add(matrix[bottom][i]); + } + bottom--; + } + if(left <= right) + { + for(int i=bottom; i>=top; i--){ + result.add(matrix[i][left]); + } + left++; + } + } + + return result; + } +} + +/* +Traverse matrix in spiral using 4 boundaries (top, bottom, left, right). +Move in 4 directions: → ↓ ← ↑, shrinking boundaries after each step. +Conditions prevent re-visiting rows/columns and avoid duplicate corners. +*/ \ No newline at end of file