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
44 changes: 44 additions & 0 deletions DiagnalTraversal.kt
Original file line number Diff line number Diff line change
@@ -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>): 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
}
}
26 changes: 26 additions & 0 deletions ProductOfArrayExceptSelf.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
47 changes: 47 additions & 0 deletions SpiralingMatrix.kt
Original file line number Diff line number Diff line change
@@ -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<IntArray>): List<Int> {
val result = mutableListOf<Int>()
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
}
}