Question: A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now given an M x N matrix, return True if
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element.
Now given an M x N matrix, return True if and only if the matrix is Toeplitz.
import java.util.Scanner; import java.lang.Math;
class ToeplizMatrix { public static void main(String[] args)
{ //Initialize scanner Scanner input = new Scanner(System.in); System.out.println("Enter rows of the matrix"); //get number of rows in a matrix int rows = input.nextInt(); System.out.println("Enter columns of the matrix"); //get number of columns in a matrix int cols = input.nextInt(); int[][] matrix = new int[rows][cols]; } public static boolean checkToepliz(int[][] matrix){
for (int i = 0; i < matrix.length - 1; i++) { for (int j = 0; j < matrix[0].length - 1; j++) { if (matrix[i][j] != matrix[i + 1][j + 1]) { return false; } } } return true; } if (checkToepliz(matrix)) { System.out.print("True"); } else { System.out.print("False"); } } }
why it is giving illegal start of type error.. howto use "if" statement in this program?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
