A vault's floor is tiled with an n×n grid of pressure-sensor readings. For a security audit you must report the total of every sensor lying on either diagonal of the grid: the main diagonal running from the top-left corner to the bottom-right corner, and the anti-diagonal running from the top-right corner to the bottom-left corner. When n is odd, the single sensor at the exact centre of the grid lies on both diagonals — count its reading only once in the total.
A single integer: the sum of every value on the main diagonal plus every value on the anti-diagonal, with the centre cell (when n is odd) counted exactly once.
Example 1
Input
3 1 2 3 4 5 6 7 8 9
Expected
25
Explanation
The main diagonal is 1, 5, 9 (sum 15). The anti-diagonal is 3, 5, 7 (sum 15). The centre cell 5 lies on both diagonals but is counted only once, so the total is 15 + 15 - 5 = 25.
Example 2
Input
1 5
Expected
5
Explanation
With n=1 the single cell lies on both the main diagonal and the anti-diagonal, but it is counted exactly once, giving a total of 5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →