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
36 changes: 36 additions & 0 deletions src/ArrayProduct.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Problem - Product of Array except self
Approach - First, we do a left-to-right pass storing running products before each index.
Then, we do a right-to-left pass multiplying the existing results with right-side products.
This way, each element gets the product of all other elements without using division.
Time Complexity - O(n)
Space Complexity - O(1)
*/

import java.util.Arrays;

public class ArrayProduct {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int [] result = new int[n];
int rp = 1; // running product
result[0] = 1;
for (int i = 1; i < n; i++) { // left traversal
rp = rp * nums[i - 1];
result[i] = rp;
}
rp = 1;
for (int i = n - 2; i >= 0; i--) { // right traversal
rp = rp * nums[i + 1];
result[i] = result[i] * rp;
}
return result;
}
public static void main(String[] args) {
ArrayProduct ap = new ArrayProduct();
int[] product1 = ap.productExceptSelf(new int[] {1,2,3,4});
int[] product2 = ap.productExceptSelf(new int[] {-1,1,0,-3,3});
System.out.println(Arrays.toString(product1));
System.out.println(Arrays.toString(product2));
}
}
62 changes: 62 additions & 0 deletions src/DiagonalMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Problem - Diagonal traverse, return array of elements in diagonal order
Approach - Start from the top-left and traverse the matrix diagonally.
Flip direction when hitting the matrix boundaries (top, bottom, left, right).
Keep updating the result array as you move in up-right or down-left directions.
Time Complexity - O(m*n)
Space Complexity - O(1)
*/


import java.util.Arrays;

public class DiagonalMatrix {
public int[] findDiagonalOrder(int[][] mat) {
int m = mat.length, n = mat[0].length;
int [] res = new int[m*n];
int r =0, c = 0;
boolean direction = true;
for(int i = 0; i < m*n; i++){
res[i] = mat[r][c];
if(direction){ // moving up
if(c == n - 1){
r++;
direction = false;
} else if (r == 0) {
c++;
direction = false;
}
else {
r--;
c++;
}
}
else{ // move down
if(r == m -1){
c++;
direction = true;
}
else if(c == 0){
r++;
direction = true;
}
else{
r++;
c--;
}
}
}

return res;
}
public static void main(String[] args) {
int[][] mat = {{1,2,3},{4,5,6},{7,8,9}};
DiagonalMatrix d = new DiagonalMatrix();
int [] res = d.findDiagonalOrder(mat);
System.out.println(Arrays.toString(res));
int[][] mat2 = {{1,2},{3,4}};
int [] res2 = d.findDiagonalOrder(mat2);
System.out.println(Arrays.toString(res2));
}

}
60 changes: 60 additions & 0 deletions src/SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Problem - Spiral , return array of elements in Spral order
Approach - We use four boundaries: top, bottom, left, right to keep track of the spiral path.
•At each step, we traverse right, down, left, then up while shrinking the boundaries.
•We stop when the boundaries cross each other.

Time Complexity - O(m*n)
Space Complexity - O(1)
*/

import java.util.ArrayList;
import java.util.List;

public class SpiralMatrix {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
int m = matrix.length;
int n = matrix[0].length;
int top = 0;
int left = 0;
int right = n - 1;
int bottom = m - 1;
while (left <= right && top <= bottom) {
// traversing left to right
for (int i = left; i <= right; i++) {
res.add(matrix[top][i]);
}
top++;
// traversing top to bottom
for (int i = top; i <= bottom; i++) {
res.add(matrix[i][right]);
}
right--;
// check if bottom is crossing top as top got mutated above and then traverse right to left
if(top <= bottom) {
for (int i = right; i >= left; i--) {
res.add(matrix[bottom][i]);
}
bottom--;
}
// check if right is crossing left as left got mutated above and then traverse bottom to top
if(left <= right) {
for (int i = bottom; i >= top; i--) {
res.add(matrix[left][i]);
}
left++;
}
} // continue until the while check fails
return res;
}
public static void main(String[] args) {
SpiralMatrix sp = new SpiralMatrix();
int[][] mat = {{1,2,3},{4,5,6},{7,8,9}};
List<Integer> res = sp.spiralOrder(mat);
System.out.println(res);
int[][] mat2 = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
List<Integer> res2 = sp.spiralOrder(mat2);
System.out.println(res2);
}
}