Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,695 questions

57,003 answers

573 users

How to check if a matrix is Toeplitz or not in Kotlin

1 Answer

0 votes
// A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements

fun isToeplitz(matrix: Array<IntArray>): Boolean {
    val rows = matrix.size
    val cols = matrix[0].size

    for (i in 1 until rows) {
        for (j in 1 until cols) {
            println("$i $j - ${matrix[i][j]} : ${matrix[i - 1][j - 1]}")
            if (matrix[i][j] != matrix[i - 1][j - 1]) {
                return false
            }
        }
        println("-----------")
    }

    return true
}

fun main() {
    val matrix = arrayOf(
        intArrayOf(2, 7, 9, 8),
        intArrayOf(4, 2, 7, 9),
        intArrayOf(3, 4, 2, 7),
        intArrayOf(0, 3, 4, 2),
        intArrayOf(6, 0, 3, 4)
    )

    if (isToeplitz(matrix)) {
        println("Matrix is a Toeplitz")
    } else {
        println("Matrix is not a Toeplitz")
    }
}

  
   
/*
run:
 
1 1 - 2 : 2
1 2 - 7 : 7
1 3 - 9 : 9
-----------
2 1 - 4 : 4
2 2 - 2 : 2
2 3 - 7 : 7
-----------
3 1 - 3 : 3
3 2 - 4 : 4
3 3 - 2 : 2
-----------
4 1 - 0 : 0
4 2 - 3 : 3
4 3 - 4 : 4
-----------
Matrix is a Toeplitz
 
*/

 



answered Jan 31, 2025 by avibootz

Related questions

1 answer 173 views
1 answer 211 views
1 answer 167 views
1 answer 203 views
1 answer 175 views
1 answer 202 views
1 answer 182 views
...