-
Notifications
You must be signed in to change notification settings - Fork 617
/
Toeplitz.java
83 lines (75 loc) · 2.45 KB
/
Toeplitz.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.
or
A Toeplitz (or diagonal-constant) matrix is a matrix in which each descending diagonal
from left to right is constant, i.e., all elements in a diagonal are same.
So, Starting from [0][0], Here We will check Elements one by one but next element will be always
in the next row and next column. if its not found as previous element then its not following Torplitz
Algorithm so, Return False else if It runs upto end with no retuen means Its Following Toeplitz algo,
So Return True.
*/
package tmatrix;
import java.util.Scanner;
public class CheckToePlitxMatrix {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
//Taking Size of Matrix
int l = scan.nextInt();
int b = scan.nextInt();
int[][] matrix = new int[l][b];
//Taking Matrix Values
for (int i = 0; i < l; i++){
for (int j = 0; j < b; j++){
matrix[i][j] = scan.nextInt();
}
}
System.out.println(isToeplitzMatrix(matrix));
}
public static boolean isToeplitzMatrix(int[][] matrix) {
int rlen = matrix.length;
int clen = matrix[0].length;
//horizontal checking
for (int i = 0; i < clen; i++){
//temp is count so that j will move till till max row length
int temp = 0;
int val = matrix[temp][i];
temp++;
for (int j = i+1; j < clen && temp < rlen; j++){
if (val != matrix[temp][j]){
return false;
}
temp++;
}
}
//vertical checking
for (int i = 1; i < rlen; i++){
//temp is count so that j will move till till max column length
int temp = 0;
int val = matrix[i][temp];
temp++;
for (int j = i+1; j < rlen && temp < clen; j++){
if (val != matrix[j][temp]){
return false;
}
temp++;
}
}
return true;
}
}
/*
Test Cases:
Input: 3
4
1 2 3 4
5 1 2 3
9 5 1 2
Output: true
Input: 2
2
1 2
2 2
Output: false
Time Complexity: O(n^2) where n is No. of Diagonal Elements in Matrix
Space Complexity: O(1)
*/