From fb32bfdfe862e19bf22b4d2cb8a5adf0e5293bc7 Mon Sep 17 00:00:00 2001 From: Sanjoli Date: Mon, 15 Jun 2026 22:18:07 -0500 Subject: [PATCH] Completed Arrays 1 --- DiagonalTraverse.java | 52 ++++++++++++++++++++++++++++++++++ ProductOfArrayExceptSelf.java | 28 ++++++++++++++++++ SpiralMatrix.java | 53 +++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 DiagonalTraverse.java create mode 100644 ProductOfArrayExceptSelf.java create mode 100644 SpiralMatrix.java diff --git a/DiagonalTraverse.java b/DiagonalTraverse.java new file mode 100644 index 00000000..3f8c086c --- /dev/null +++ b/DiagonalTraverse.java @@ -0,0 +1,52 @@ +class Solution { + public int[] findDiagonalOrder(int[][] mat) { + // TC - O(m*n) + // SC - O(m*n) + /* + To traverse matrix diagonally, we need to keep track of the direction we are moving in and what diagonal index to move to next. + We use flag to keep track of direction, so flag is true when e move up the matrix and false otherwise. + If flag is true and we reach the topmost row set the flag to false and incr column index + */ + int n = mat.length; + int m = mat[0].length; + + int[] result = new int[n*m]; + int r = 0, c = 0; + boolean flag = true; + + for (int i = 0; i< n*m; i++) { + result[i]= mat[r][c]; + if (flag) { + //If flag is true and we reach the topmost row and column is not the last, set the flag to false and incr column index + if (r == 0 && c != m - 1) { + flag = false; + c++; + } else if (c == m - 1) { + //If flag is true and we reach the last column, set the flag to false and incr row index + flag = false; + r++; + } else { + // else we keep moving up diagonally by decr row and incr col + r--; + c++; + } + } else { + if (c == 0 && r != n - 1) { + //If flag is false and we reach the topmost column and row is not the last, set the flag to true and incr row index + r++; + flag = true; + } else if (r == n - 1) { + //If flag is false and we reach the last row, set the flag to true and incr col index + flag = true; + c++; + } else { + // else we keep moving down diagonally by incr row and decr col + r++; + c--; + } + } + } + + return result; + } +} \ No newline at end of file diff --git a/ProductOfArrayExceptSelf.java b/ProductOfArrayExceptSelf.java new file mode 100644 index 00000000..a02863cf --- /dev/null +++ b/ProductOfArrayExceptSelf.java @@ -0,0 +1,28 @@ +class Solution { + public int[] productExceptSelf(int[] nums) { + // TC - O(n) + // SC - O(1) (not including result array) + /* + To calculate product of all elements except self, we first run a loop from left to right + and calculate prouduct with each element to the left (which will be stored at index - 1 as we have already calculated that). + And then we pass the array right to left and keep a runningSum initialize with the rightmost element and then keep + multiplying it with values at the index. We also keep multiplying the runningSum value at each index. + */ + + int[] product = new int[nums.length]; + product[0] = 1; + + for (int i = 1; i < nums.length; i++) { + product[i] = nums[i - 1] * product[i - 1]; + } + + int runningProduct = nums[nums.length - 1]; + + for (int i = nums.length - 2; i >= 0; i--) { + product[i] = product[i] * runningProduct; + runningProduct *= nums[i]; + } + + return product; + } +} \ No newline at end of file diff --git a/SpiralMatrix.java b/SpiralMatrix.java new file mode 100644 index 00000000..b7f80722 --- /dev/null +++ b/SpiralMatrix.java @@ -0,0 +1,53 @@ +import java.util.*; + +class Solution { + public List spiralOrder(int[][] matrix) { + // TC - O(m*n) + // SC - O(m*n) + /* + To visit matrix in a spiral we take 4 pointers top, bottom, left, and right. We iterate from left to right (print top row and incr top pointer) + , then top to bottom (print right column and decrement right pointer), right to left (print bottom row and decr bottom pointer) and + bottom to top (print left row and incr left pointer) in each loop. + */ + List list = new ArrayList<>(); + int n = matrix.length; + int m = matrix[0].length; + int top = 0; + int bottom = n - 1; + int left = 0; + int right = m - 1; + + // termination condition if top and bottom cross or right and left pointers cross + while (top <= bottom && left <= right) { + //print top row and incr top pointer + for (int j = left; j <= right; j++) { + list.add(matrix[top][j]); + } + top++; + + //print right column and decrement right pointer + for (int i = top; i <= bottom; i++) { + list.add(matrix[i][right]); + } + right--; + + //check if top and bottom not invalid and print bottom row and decr bottom pointer + if (top <= bottom) { + for (int j = right; j >= left; j--) { + list.add(matrix[bottom][j]); + } + bottom--; + } + + //check if left and right not invalid and print left row and incr left pointer + if (left <= right) { + for (int i = bottom; i >= top; i--) { + list.add(matrix[i][left]); + } + left++; + } + } + + return list; + } +} \ No newline at end of file