-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVestigium.java
executable file
·75 lines (66 loc) · 2.19 KB
/
Vestigium.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
import java.util.*;
public class Vestigium {
// Function to check the validity of matrix
private static void trace(int mat[][], int N) {
int trace = 0;
// Calculate trace
for (int i = 0; i < N; ++i) {
trace += mat[i][i];
}
// Row order scan to count repeated elements
int rowCount = 0;
for (int r = 0; r < N; ++r) {
// Array to maintain frequency of each element
int frequency[] = new int[N];
for (int c = 0; c < N; ++c) {
frequency[mat[r][c] - 1]++;
if (frequency[mat[r][c] - 1] > 1) {
// repeated element
rowCount++;
break;
}
}
}
// Column order scan to count repeated elements
int colCount = 0;
for (int c = 0; c < N; ++c) {
// Array to maintain frequency of each element
int frequency[] = new int[N];
for (int r = 0; r < N; ++r) {
frequency[mat[r][c] - 1]++;
if (frequency[mat[r][c] - 1] > 1) {
// repeated element
colCount++;
break;
}
}
}
// Print result
System.out.println(trace + " " + rowCount + " " + colCount);
}
// Helper function to call comptuing function and print result in desired format
private static void executeTestCase(int mat[][], int N, int t) {
if (mat == null || (mat.length != mat[0].length) ||
(mat.length != N)) {
return;
}
System.out.print("Case #" + t + ": ");
trace(mat, N);
}
public static void main(String []args){
// Read inputs
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for (int i = 1; i <= t; ++i) {
int n = sc.nextInt();
int mat[][] = new int[n][n];
for (int r = 0; r < n; ++r) {
for (int c = 0; c < n; ++c) {
mat[r][c] = sc.nextInt();
}
}
// Call helper method
executeTestCase(mat, n, i);
}
}
}