From 685ba5370c4de873016fdb79f2a0c388bedddb1c Mon Sep 17 00:00:00 2001 From: gurneetk186 Date: Wed, 26 Nov 2025 11:35:33 -0800 Subject: [PATCH] Done Array-1 --- Problem1.java | 33 ++++++++++++++++++++++++++++++ Problem2.java | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ Problem3.java | 41 +++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 Problem1.java create mode 100644 Problem2.java create mode 100644 Problem3.java diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..25b2fc0d --- /dev/null +++ b/Problem1.java @@ -0,0 +1,33 @@ +//Time Complexity : O(n) +//Space Complexity : O(1) // output array not counted +//Did this code successfully run on Leetcode : Yes +//First, we create a result list where each position will store the final answer. + +//Then we do a left pass: for every index, we multiply all numbers to its left and store that in result. + +//Next, we do a right pass: we multiply everything to its right and add it to the result. + +//Combining left × right gives the final answer for each position without using division. +class Solution { + public int[] productExceptSelf(int[] nums) { + + int n = nums.length; + int[] result = new int[n]; + + // LEFT PASS + result[0] = 1; + for (int i = 1; i < n; i++) { + result[i] = result[i - 1] * nums[i - 1]; + } + + // RIGHT PASS + int rightProduct = 1; + for (int i = n - 1; i >= 0; i--) { + result[i] = result[i] * rightProduct; + rightProduct = rightProduct * nums[i]; + } + + return result; + } +} + diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..75c885c3 --- /dev/null +++ b/Problem2.java @@ -0,0 +1,56 @@ +//Time Complexity : O(m * n) +//Space Complexity : O(1) // excluding the result array +//Did this code successfully run on Leetcode : Yes +// We move through the matrix diagonally in two directions: up-right and down-left. +// Whenever we hit a boundary (top, bottom, left, or right), we switch direction. +// We keep updating the row and column based on the current direction until all elements are added. +// This allows us to traverse the entire matrix in the required diagonal zig-zag order. +class Solution { + public int[] findDiagonalOrder(int[][] mat) { + int m = mat.length; + int n = mat[0].length; + + int[] result = new int[m * n]; + int r = 0, c = 0; + boolean up = true; // true = up-right, false = down-left + + for (int i = 0; i < m * n; i++) { + + result[i] = mat[r][c]; // add current element + + if (up) { // moving up-right ↗ + + if (c == n - 1) { // hit RIGHT wall + r++; // move DOWN + up = false; // change direction + } + else if (r == 0) { // hit TOP wall + c++; // move RIGHT + up = false; // change direction + } + else { // normal up-right move + r--; + c++; + } + + } else { // moving down-left ↘ + + if (r == m - 1) { // hit BOTTOM wall + c++; // move RIGHT + up = true; // change direction + } + else if (c == 0) { // hit LEFT wall + r++; // move DOWN + up = true; // change direction + } + else { // normal down-left move + r++; + c--; + } + } + } + + return result; + } +} + diff --git a/Problem3.java b/Problem3.java new file mode 100644 index 00000000..7c34bf79 --- /dev/null +++ b/Problem3.java @@ -0,0 +1,41 @@ +// We maintain four boundaries: top, bottom, left, and right. +// In each loop, we traverse the matrix in four directions: left→right, top→bottom, right→left, and bottom→top. +// After completing one spiral layer, we shrink the boundaries inward. +// We repeat this until all rows and columns are processed in spiral order. +//Time Complexity : O(m * n) +//Space Complexity : O(1) // excluding result list +//Did this code successfully run on Leetcode : Yes +class Solution { + public List spiralOrder(int[][] matrix) { + int m = matrix.length;//no. of rows + int n = matrix[0].length;//no.of columns + int top = 0, bottom = m-1, left = 0, right = n-1; + List result = new ArrayList<>(); + while (top <= bottom && left <= right){ + 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; +} +}