Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Problem1.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

56 changes: 56 additions & 0 deletions Problem2.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

41 changes: 41 additions & 0 deletions Problem3.java
Original file line number Diff line number Diff line change
@@ -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<Integer> 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<Integer> 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;
}
}