← Unit 4 · all exercises
Saved in this browser only

Down the diagonal

The grid is square. Return the total of the items running from the top left to the bottom right — positions [0][0], [1][1] and so on.

int diagonal(int[][] grid)

Solution.java
Loading editor…
Results

Examples

  • Solution.diagonal(new int[][]{{1, 2}, {3, 4}}) → 5
  • Solution.diagonal(new int[][]{{9}}) → 9

There are also 2 hidden tests you can't see. Solve the problem, not the examples.