Consider summing the four orthogonal neighbors of <code>m[r][c]</code> ignoring out-of-bounds:
<code class="language-java">int neigh(int[][] m, int r, int c) { int s = 0; int[] dr = {-1,1,0,0}, dc = {0,0,-1,1}; for (int k = 0; k < 4; k++) { int nr = r + dr[k], nc = c + dc[k]; if (nr >= 0 && nr < m.length && nc >= 0 && nc < m[0].length) s += m[nr][nc]; } return s; } </code></pre> With the highlighted target <code>m[0][0]</code>, what does <code>neigh(m,0,0)</code> return?
- A
ArrayIndexOutOfBoundsException
- Bcheck_circle
5
- C
13
- D
10
Explanation
Top neighbor (-1,0) and left neighbor (0,-1) are out of bounds and skipped. Down = m[1][0] = 3, right = m[0][1] = 2. Sum = 3 + 2 = 5.