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
17 changes: 17 additions & 0 deletions Product_except_self.java
Original file line number Diff line number Diff line change
@@ -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<nums.length;i++){
rp = rp * nums[i-1];
res[i] = rp;
}
rp=1;
for(int i=nums.length-2;i>=0;i--){
rp = rp * nums[i+1];
res[i] = res[i] * rp;
}
return res;
}
}
80 changes: 80 additions & 0 deletions diagonal_traverse.java
Original file line number Diff line number Diff line change
@@ -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)

======================================================
*/
39 changes: 39 additions & 0 deletions spiral_matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int top =0, bottom =m-1, left =0, right = n-1;
List<Integer> 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.
*/