Learn
Exam

Delta Search in a Two-Dimensional Array 1

Difficulty
BEGINNER

Problem

Given a n x m two-dimensional array numbers, a row index r, and a column index c, write a function solution that returns the sum of the elements adjacent to the element at the r-th row and c-th column of numbers in the up, down, left, and right directions.


Constraints

  • The dimensions of numbers (n and m) are between 2 and 20 inclusive. n and m can be different.
  • Each element numbers[i][j] is a natural number between 1 and 1,000 inclusive.
  • r is a natural number between 1 and n inclusive.
  • c is a natural number between 1 and m inclusive.

Test Case

Test Case Number numbers r c return
1 [[5,7,14],[3,3,11],[14,10,9],[6,15,7],[15,14,11]] 2 2 31
2 [[9,12],[1,6],[9,3],[15,14]] 4 2 18
3 [[3,4,12],[10,9,9],[5,2,8]] 2 1 17

Explanation for Test Case 1

  • The situation given in the problem is illustrated as follows:
  • The selected cell at row 2, column 2 has adjacent elements 7, 10, 3, and 11. Their total is 31.

Explanation for Test Case 2

  • The situation given in the problem is illustrated as follows:
  • The selected cell at row 4, column 2 has no adjacent elements below or to the right. The adjacent elements above and to the left are 3 and 15, respectively. Their total is 18.

Explanation for Test Case 3

  • The situation given in the problem is illustrated as follows:
  • The selected cell at row 2, column 1 has no adjacent elements to the left. The adjacent elements above, below, and to the right are 3, 5, and 9, respectively. Their total is 17.