We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Diagonal Difference
- Discussions
Diagonal Difference
Diagonal Difference
Sort by
recency
|
670 Discussions
|
Please Login in order to post a comment
Python:
Only read if you have attempted to solve the problem
The key to solving this problem is finding the relationship between the row and column index positions when iterating through the 2d array.
For the main diagonal, the row and column indices will always be equal.
For the secondary diagonal, the column index will decrease as each row increases. This relationship can be described as col_index = (arr.size() - row_index - 1).
Python:
def diagonalDifference(arr): left_diagonal = [] right_diagonal = [] ratio = 0 for i, row in enumerate(arr): last = len(row) -1 - ratio ratio += 1 for j, elem in enumerate(row): if i == j: left_diagonal.append(arr[i][j]) if j == last: right_diagonal.append(arr[i][j])