To solve the problem, I used an approach based on counting the number of servers in each row and column. First, I traversed the grid to calculate two arrays: row_count
and col_count
. row_count[i]
stores the number of servers in row i
, and col_count[j]
stores the number of servers in column j
.
Next, I performed a second traversal of the grid. For each server (grid[i][j] == 1
), I checked if there were other servers in its row or column by evaluating whether row_count[i] > 1
or col_count[j] > 1
. If either condition was true, the server was added to the count of communicating servers - res
.