diff --git a/DiagnalTraversal.kt b/DiagnalTraversal.kt new file mode 100644 index 00000000..10ba5202 --- /dev/null +++ b/DiagnalTraversal.kt @@ -0,0 +1,44 @@ +//In this problem we need to traverse a 2d matrix in a diagonal order and return the elements in an array. We use a flag to determine the direction of traversal, either upwards or downwards. We also handle boundary conditions to ensure we don't go out of bounds while traversing the matrix. +// Time complexity is O(m*n) where m is number of rows and n is number of columns. Space complexity is O(1) + +class Solution { + fun findDiagonalOrder(mat: Array): IntArray { + val m = mat.size + val n = mat[0].size + + val result = IntArray(m*n) + var flag = true //up + var r = 0 + var c = 0 + for(i in 0..result.size-1) { + result[i] = mat[r][c] + + if(flag) { + if(c == n - 1) { + r++; + flag = false; + } + else if(r == 0) { + c++; + flag = false; + } + else { + r--; c++; + } + } else { + if(r == m - 1) { + c++; + flag = true; + } + else if(c == 0) { + r++; + flag = true; + } + else { + r++; c--; + } + } + } + return result + } +} \ No newline at end of file diff --git a/ProductOfArrayExceptSelf.kt b/ProductOfArrayExceptSelf.kt new file mode 100644 index 00000000..f1a6cb24 --- /dev/null +++ b/ProductOfArrayExceptSelf.kt @@ -0,0 +1,26 @@ +// In this problem we need to find the product of all elements in the array except for the current index. We do this in two passes, first calculating the product of all elements to the left of each index and store it in the output array. +// In the second pass, we calculate the product of all elements to the right of each index and multiply it with the corresponding value in the output array. +// Time complexity is O(n) where n is the number of elements in the array. Space complexity is O(1) + +class Solution { + fun productExceptSelf(nums: IntArray): IntArray { + + val n = nums.size + var rp = 1 + var output = IntArray(nums.size) + output[0] = 1 + //first pass 1 to right + for (i in 1 .. n-1) { + rp = rp * nums[i -1] + output[i] = rp + } + + rp = 1 + for(i in n-2 downTo 0) { + rp = rp * nums[i+ 1] + output[i] = output[i] * rp + } + + return output + } +} \ No newline at end of file diff --git a/SpiralingMatrix.kt b/SpiralingMatrix.kt new file mode 100644 index 00000000..d7a855b9 --- /dev/null +++ b/SpiralingMatrix.kt @@ -0,0 +1,47 @@ +// In this problem we need to traverse a 2d matrix in a spiral order and return the elements in a list.We have add consditions on all the sides of the matrix which top, right, bottom and left. +// we also take care of breach scenarios while inside the while loop so that we don't accidentally collide the sides. +// Time complexity is O(m*n) where m is number of rows and n is number of columns. Space complexity is O(1) + +class Solution { + fun spiralOrder(matrix: Array): List { + val result = mutableListOf() + val m = matrix.size + val n = matrix[0].size + var top = 0 + var left = 0 + var right = n - 1 + var bottom = m - 1 + + while (top <= bottom && left <= right) { + //top row + for (i in left..right) { + result.add(matrix[top][i]) + } + top++ + + if (top <= bottom && left <= right) { + for(i in top..bottom){ + result.add(matrix[i][right]) + } + right-- + } + + if (top <= bottom && left <= right){ + + for(i in right downTo left) { + result.add(matrix[bottom][i]) + } + bottom-- + } + if (top <= bottom && left <= right){ + + for(i in bottom downTo top) { + result.add(matrix[i][left]) + } + left++ + } + } + + return result + } +} \ No newline at end of file