Given a 2D array that has 1s & 0s where 1 denotes a server, return the count of the number of servers that communicate with other servers. A server can communicate with another server only if they are in the same row/column.
For example: [ [1, 0],
[1, 1] ] output = 3
const countLiveServers = (inp) => {
const rows = Array(inp.length).fill(0);
const cols = Array(inp[0].length).fill(0);
for (let i = 0; i < inp.length; i++) {
for (let j = 0; j < inp[0].length; j++) {
if (inp[i][j]) {
rows[i]++;
cols[j]++;
}
}
}
let liveServerCount = 0;
for (let i = 0; i < inp.length; i++) {
for (let j = 0; j < inp[0].length; j++) {
if (inp[i][j] && (rows[i] > 1 || cols[j] > 1)) {
liveServerCount++;
}
}
}
return liveServerCount;
};
console.log(countLiveServers([[1,0],[1,1]])); // 3