Spiral Matrix Traversal in Kotlin

A spiral matrix is a matrix of m rows and n columns with each cell containing an integer value. The traversal of a spiral matrix is a way of visiting every cell in the matrix in a spiral order, starting from the top-left corner and moving in a clockwise direction.

To traverse a spiral matrix, we start by visiting the top row of the matrix from left to right. Next, we visit the rightmost column of the matrix from top to bottom. Then, we visit the bottom row of the matrix from right to left. Finally, we visit the leftmost column of the matrix from bottom to top. We repeat this process for the remaining inner submatrix until there are no more cells left to visit.

Here is an example of a 4×4 spiral matrix and its traversal:

Matrix:
[ 1 2 3 4 ]
[ 12 13 14 5 ]
[ 11 16 15 6 ]
[ 10 9 8 7 ]

Traversal: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

The traversal order starts at the top-left corner, moves right to the end of the first row, then down to the last column, left to the second-to-last row, up to the second column, and so on until all cells have been visited.

Kotlin
fun spiralTraversal(matrix: Array<IntArray>): List<Int> {
    val m = matrix.size
    val n = matrix[0].size
    val res = mutableListOf<Int>()
    
    var rowStart = 0
    var rowEnd = m - 1
    var colStart = 0
    var colEnd = n - 1
    
    while (rowStart <= rowEnd && colStart <= colEnd) {
        // Traverse the top row from left to right
        for (i in colStart..colEnd) {
            res.add(matrix[rowStart][i])
        }
        rowStart++
        
        // Traverse the right column from top to bottom
        for (i in rowStart..rowEnd) {
            res.add(matrix[i][colEnd])
        }
        colEnd--
        
        // Traverse the bottom row from right to left
        if (rowStart <= rowEnd) {
            for (i in colEnd downTo colStart) {
                res.add(matrix[rowEnd][i])
            }
            rowEnd--
        }
        
        // Traverse the left column from bottom to top
        if (colStart <= colEnd) {
            for (i in rowEnd downTo rowStart) {
                res.add(matrix[i][colStart])
            }
            colStart++
        }
    }
    
    return res
}
  1. First, we create an empty mutable list result to store the spiral order of the matrix. If the input matrix is empty, we immediately return the empty result.
  2. We then set up four variables top, bottom, left, and right to represent the boundaries of the matrix. We initialize top and left to 0, and bottom and right to the last row and column of the matrix respectively.
  3. We start a while loop that runs as long as top is less than or equal to bottom, and left is less than or equal to right. This loop will traverse the matrix in a spiral pattern.
  4. Inside the loop, we use a for loop to traverse the top row of the current spiral, from left to right. We add each element to the result list.
  5. We increment top to move down to the next row of the spiral.
  6. We use another for loop to traverse the right column of the current spiral, from top to bottom. We add each element to the result list.
  7. We decrement right to move left to the next column of the spiral.
  8. We use an if statement to check if there are more rows in the spiral. If so, we use a for loop to traverse the bottom row of the current spiral, from right to left. We add each element to the result list.
  9. We decrement bottom to move up to the next row of the spiral.
  10. We use another if statement to check if there are more columns in the spiral. If so, we use a for loop to traverse the left column of the current spiral, from bottom to top. We add each element to the result list.
  11. We increment left to move right to the next column of the spiral.
  12. The while loop continues until all elements in the matrix have been traversed. At the end, we return the result list containing the spiral order of the matrix.
Kotlin
fun spiralTraversal(matrix: Array<IntArray>): List<Int> {
    val m = matrix.size
    val n = matrix[0].size
    val res = mutableListOf<Int>()
    
    var rowStart = 0
    var rowEnd = m - 1
    var colStart = 0
    var colEnd = n - 1
    
    while (rowStart <= rowEnd && colStart <= colEnd) {
        // Traverse the top row from left to right
        for (i in colStart..colEnd) {
            res.add(matrix[rowStart][i])
        }
        rowStart++
        
        // Traverse the right column from top to bottom
        for (i in rowStart..rowEnd) {
            res.add(matrix[i][colEnd])
        }
        colEnd--
        
        // Traverse the bottom row from right to left
        if (rowStart <= rowEnd) {
            for (i in colEnd downTo colStart) {
                res.add(matrix[rowEnd][i])
            }
            rowEnd--
        }
        
        // Traverse the left column from bottom to top
        if (colStart <= colEnd) {
            for (i in rowEnd downTo rowStart) {
                res.add(matrix[i][colStart])
            }
            colStart++
        }
    }
    
    return res
}
fun main() {
    val matrix = arrayOf(
        intArrayOf(1, 2, 3, 4),
        intArrayOf(5, 6, 7, 8),
        intArrayOf(9, 10, 11, 12),
        intArrayOf(13, 14, 15, 16)
    )
    val result = spiralTraversal(matrix)
    println(result.joinToString(", "))
}

Output : 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10

Leave a Comment