![Loop Through The Diagonal Elements In Two Dimensional Array Loop Through The Diagonal Elements In Two Dimensional Array](/wp-content/uploads/2019/05/Loop-Through-Diagonal-Elements-In-Two-Dimensional-Array.png)
If you need to only loop through the diagonal elements of the two-dimensional array you can use the following C# code (but should be more or less the same for any programming language). It calculates the diagonal index based on a simple formula and has O(n) complexity, where n = max(width, height)