Count Servers that Communicate - Problem

You are given a map of a server center, represented as a m × n integer matrix grid, where 1 means that on that cell there is a server and 0 means that there is no server.

Two servers are said to communicate if they are on the same row or on the same column.

Return the number of servers that communicate with any other server.

Input & Output

Example 1 — Basic Case
$ Input: grid = [[1,0],[0,1]]
Output: 0
💡 Note: No server can communicate with any other server. Server at (0,0) has no other server in row 0 or column 0. Server at (1,1) has no other server in row 1 or column 1.
Example 2 — Same Row Communication
$ Input: grid = [[1,0],[1,1]]
Output: 3
💡 Note: Server (1,0) and (1,1) can communicate (same row). Server (0,0) and (1,0) can communicate (same column). All 3 servers can communicate.
Example 3 — Mixed Communication
$ Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
Output: 4
💡 Note: Servers (0,0) and (0,1) communicate via same row. Servers (1,2) and (2,2) communicate via same column. Server (3,3) is isolated. Total: 4 servers communicate.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m ≤ 250
  • 1 ≤ n ≤ 250
  • grid[i][j] == 0 or 1

Visualization

Tap to expand
Count Servers that Communicate101010101Row 0: 2 serversCol 0Col 2Communication Rules:✓ Servers in same row can communicate✓ Servers in same column can communicate✗ Isolated servers cannot communicateAnalysis:• Row 0: 2 servers → both communicate• Row 1: 1 server → isolated• Row 2: 2 servers → both communicateResult: 4 out of 5 servers can communicateGreen servers communicate, Orange server is isolated
Understanding the Visualization
1
Input Grid
Matrix with 1s representing servers, 0s representing empty cells
2
Communication Rules
Servers communicate if they share the same row or column
3
Count Result
Number of servers that can communicate with at least one other server
Key Takeaway
🎯 Key Insight: A server communicates if its row or column contains more than one server
Asked in
Google 15 Amazon 12 Facebook 8 Microsoft 6
32.1K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen