#C10881. Maximum Element in Diagonals of a Square Matrix
Maximum Element in Diagonals of a Square Matrix
Maximum Element in Diagonals of a Square Matrix
You are given a square matrix of size \(n \times n\) containing integers. Your task is to determine the largest integer that appears on any of the diagonals of the matrix. Here, a diagonal is defined in two ways:
- Main diagonals: Diagonals that run from the top-left to the bottom-right. For example, if a diagonal starts at \( (0, j) \) or \( (i, 0) \), it continues by incrementing both row and column indices.
- Anti-diagonals: Diagonals that run from the top-right to the bottom-left. These can start from the top row or the rightmost column and continue by incrementing the row index while decrementing the column index.
Your program should read the input from stdin
and write the result to stdout
.
Note: All formulas in this description are given in LaTeX format.
inputFormat
The input is provided on stdin
in the following format:
n row1_element1 row1_element2 ... row1_elementn row2_element1 row2_element2 ... row2_elementn ... rown_element1 rown_element2 ... rown_elementn
Here, \(n\) is the size of the matrix and each row of the matrix contains \(n\) space-separated integers.
outputFormat
The output is a single integer printed to stdout
, representing the largest integer found on any of the specified diagonals of the matrix.
3
1 2 3
4 5 6
7 8 9
9