-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem1267.java
More file actions
51 lines (50 loc) · 1.4 KB
/
Problem1267.java
File metadata and controls
51 lines (50 loc) · 1.4 KB
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
import java.util.Scanner;
public class Problem1267 {
// Count Servers that Communicate
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
int t=obj.nextInt();
obj.nextLine();
while(t-->0){
int row=obj.nextInt();
int col=obj.nextInt();
int mat[][]=new int[row][col];
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
mat[i][j]=obj.nextInt();
}
}
Solution1267 sc=new Solution1267();
System.out.println(sc.countServer(mat));
}
obj.close();
}
}
class Solution1267{
public int countServer(int mat[][]){
int row=mat.length;
int col=mat[0].length;
int rowCount[]=new int[row];
int colCount[]=new int[col];
int totalServer=0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(mat[i][j]==1){
rowCount[i]++;
colCount[j]++;
totalServer++;
}
}
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(mat[i][j]==1){
if(!(rowCount[i]>1 || colCount[j]>1)){
totalServer--;
}
}
}
}
return totalServer;
}
}