![]() |
VOOZH | about |
Given a square matrix mat[][] of odd dimensions, find the sum of the elements in the middle row and the middle column.
Examples:
Input : mat[][] = [[2, 5, 7],
[3, 7, 2],
[5, 6, 9]]
Output : [12, 18]
Explanation: Middle row [3, 7, 2] sums to 12, middle column [5, 7, 6] sums to 18.Input : mat[][] = [[1, 3, 5, 6, 7],
[3, 5, 3, 2, 1],
[1, 2, 3, 4, 5],
[7, 9, 2, 1, 6],
[9, 1, 5, 3, 2]]
Output : [15, 18]
Explanation: Middle row [1, 2, 3, 4, 5] sums to 15, middle column [5, 3, 3, 2, 5] sums to 18.
We will first find the middle index mid = n / 2. We then sum all elements in the middle row and all elements in the middle column to get rowSum and colSum. Finally, we return [rowSum, colSum].
12 18