Check Toeplitz Matrix

A Toeplitz matrix is also known as a diagonal-constant matrix is a special type of matrix in which the diagonal from top left to bottom right is constant.

package org.wesome.dsalgo;

import java.util.Objects;

public class ToeplitzMatrix {
    public static boolean isToeplitzMatrix(int[][] matrix, int ROW, int COL) {
        if (Objects.isNull(matrix)) {
            return true;
        }
        for (int row = 0, col = 0; row < ROW - 1 && col < COL - 1; row++, col++) {
            if (matrix[row][col] != matrix[row + 1][col + 1]) {
                return false;
            }
        }
        return true;
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.wesome.dsalgo.ToeplitzMatrix.isToeplitzMatrix;

public class SortedMatrixCountTest {
    @Test
    void toeplitzMatrixTest1() {
        int[][] matrix = {
                {4, 6, 1, 9, 3},
                {6, 4, 2, 1, 8},
                {9, 1, 4, 9, 2},
                {2, 9, 2, 4, 7}
        };
        int ROW = 4;
        int COL = 5;
        Assertions.assertTrue(isToeplitzMatrix(matrix, ROW, COL));
    }

    @Test
    void toeplitzMatrixTest2() {
        int[][] matrix = {
                {1, 1, 1, 1},
                {1, 1, 1, 1},
                {1, 1, 1, 1},
                {1, 1, 1, 1}
        };
        int ROW = 3;
        int COL = 4;
        Assertions.assertTrue(isToeplitzMatrix(matrix, ROW, COL));
    }
}
plugins {
    id 'java'
    id "io.freefair.lombok" version "6.2.0"
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}

test {
    useJUnitPlatform()
}

 

follow us on