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
45 changes: 45 additions & 0 deletions ProductExceptSelf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Time Complexity : O(n)
// Space Complexity : O(1) extra space (excluding the output array)
// We use the output array to store results, and use only a couple of variables (lp) for computation.
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
// Your code here along with comments explaining your approach:
// First, I store the product of all elements to the left of each index in the output array.
// Then, I go from right to left, and multiply the existing output value with the product of elements to the right.
// This gives the final result where each element is the product of all other elements except itself.


import java.util.Arrays;

public class ProductExceptSelf {
public int[] solve(int[] nums){
if(nums==null || nums.length==0){
return new int[] {};
}
int n = nums.length;
int lp=1;
int [] arr = new int[n];
arr[0] = lp;
for(int i=1; i<n; i++){
lp = lp * nums[i-1];
arr[i] = lp;
}
lp=1;
for(int i=n-2;i>=0;i--){
lp = lp * nums[i+1];
arr[i] = arr[i]*lp;

}

return arr;
}
public static void main(String[]args){
ProductExceptSelf ob = new ProductExceptSelf();
int[] nums1 = {1,2,3,4};
int[] nums2 = {-1,1,0,-3,3};
System.out.println("Product of array except self is : "+ Arrays.toString(ob.solve(nums1)));
System.out.println("Product of array except self is : "+ Arrays.toString(ob.solve(nums2)));

}

}
85 changes: 85 additions & 0 deletions SpiralOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Time Complexity : O(m * n), m=rows, n=cols
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english:
// We define 4 boundaries (top, bottom, left, right) and keep updating them as we traverse.
// At each step, we go left→right, top→bottom, right→left, and bottom→top, collecting elements in spiral order.
// We continue this until all elements are visited (i.e., top > bottom or left > right).


import java.util.*;
public class SpiralOrder {
public List<Integer> solve(int[][] matrix){
if(matrix==null || matrix.length==0){
return new ArrayList<>();
}
List<Integer> res = new ArrayList<>();
int m = matrix.length;
int n = matrix[0].length;
int top = 0;
int bottom = m-1;
int left = 0;
int right = n-1;

while(top<=bottom && left<=right){

// Move from left to right
if(top<=bottom && left<=right){
for(int i=left; i<=right; i++){
res.add(matrix[top][i]);
}
top++;
}
//Move from top to bottom
if(top<=bottom && left<=right){
for(int i=top; i<=bottom; i++){
res.add(matrix[i][right]);
}
right--;
}
//Move from right to left
if(top<=bottom && left<=right){
for(int i = right; i>=left;i--){
res.add(matrix[bottom][i]);

}
bottom--;
}
//Move from bottom to top
if(top<=bottom && left<=right){
for(int i=bottom; i>=top;i--){
res.add(matrix[i][left]);
}
left++;
}


}
return res;


}
public static void main(String[]args){
SpiralOrder ob = new SpiralOrder();
int[][] matrix = {
{1,2,3},
{4,5,6},
{7,8,9}
};
int[][] matrix2 = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12}
};

List<Integer> res = ob.solve(matrix);
List<Integer> res2 = ob.solve(matrix2);

System.out.println("Sprial order is: "+res);
System.out.println("Spiral order is: "+res2);



}

}
82 changes: 82 additions & 0 deletions findDiagonalOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Time Complexity : O(m * n)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Three line explanation of solution in plain english:
// We use a direction variable (1 for upward, -1 for downward) to zigzag through the matrix.
// Based on current row and column, we decide the next position and when to change direction.
// We keep collecting elements into a result array until we’ve covered all matrix cells.


import java.util.Arrays;

public class findDiagonalOrder {
public int[] solve(int[][] mat){
if(mat==null || mat.length==0){
return new int[] {};
}
int m = mat.length;
int n = mat[0].length;
int[] res = new int[m * n];
int idx = 0;
int row = 0;
int col = 0;
int dir = 1;

while(idx<m*n){
res[idx] = mat[row][col];
idx++;
if(dir == 1){
if(col==n-1){
dir=-1;
row++;
}
else if(row==0){
dir=-1;
col++;

}
else{
row--;
col++;
}

}
else{
if(row == m-1){
dir = 1;
col++;

}
else if(col == 0){
dir=1;
row++;
}
else{
row++;
col--;
}

}
}
return res;


}
public static void main(String[]args){
findDiagonalOrder ob = new findDiagonalOrder();
int[][] mat1 = {
{1,2,3},
{4,5,6},
{7,8,9}
};
int[][] mat2 = {
{1,2},
{3,4}
};
int [] res = ob.solve(mat1);
int [] res2 = ob.solve(mat2);
System.out.println("Diagonal Order :" + Arrays.toString(res));
System.out.println("Diagonal Order :" + Arrays.toString(res2));
}

}