diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..59735d2b --- /dev/null +++ b/Problem1.java @@ -0,0 +1,21 @@ +class Solution { + public int[] productExceptSelf(int[] nums) { + int rp=1; + int n= nums.length; + // forward pass left to right + int [] result = new int[n]; + result[0]=1; + for(int i=1;i=0;i--){ + rp=rp*nums[i+1]; + result[i]=rp*result[i]; + } + return result; + } +} \ No newline at end of file diff --git a/Problem2.java b/Problem2.java new file mode 100644 index 00000000..6e22e205 --- /dev/null +++ b/Problem2.java @@ -0,0 +1,57 @@ +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; + int c=0; + boolean flag =true; //upward + for(int i=0;i spiralOrder(int[][] matrix) { + List li= 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){ + //top row iteration + for(int j=left; j<=right; j++){ + li.add(matrix[top][j]); + } + //shrinking the top + top++; + if(top<=bottom){ + //right col iteration + for(int i=top; i<=bottom; i++){ + li.add(matrix[i][right]); + } + } + //shrinking right + right--; + if(top<=bottom && left<=right){ + //bottom row iteration + for(int j=right; j>=left; j--){ + li.add(matrix[bottom][j]); + } + } + //shrinking bottom + bottom--; + if(top<=bottom && left<=right){ + //left col iteration + for(int i=bottom; i>=top; i--){ + li.add(matrix[i][left]); + } + } + //shrinking left + left++; + } + return li; + + } +} \ No newline at end of file