Skip to the code
cf.
+10 XP

#76 Diagonal Totals of a Board

easy

A tic-tac-toe style game scores a square board grid by adding the numbers on both diagonals: the one from top-left to bottom-right and the one from top-right to bottom-left.

Write diagonalTotal(grid) that returns that total. If the board has an odd size, the center cell is on both diagonals but counts only once.

Example 1

Input: grid = [[2,0,1],[0,4,0],[3,0,5]]
Output: 15

2 + 4 + 5 on one diagonal and 1 + 3 on the other; the center 4 is counted once.

Example 2

Input: grid = [[1,1],[1,1]]
Output: 4

An even board has no shared center, so all four cells count.

Constraints

  • 1 <= n <= 100
  • -100 <= grid[r][c] <= 100
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).