# Diagonal traversal of a matrix

Given a matrix [RXC] size, Our goal is to print its diagonal elements. There are many variants of diagonal view. This post is based on print element in top to bottom direction and left to right. Let see an example.

``````// Java program
// Diagonal traversal of a matrix
public class DiagonalTraversal
{
public void diagonalView(int[][] data)
{
// Get the size
int row = data.length;
int col = data[0].length;
// First Half
for (int i = 0; i < col; ++i)
{
for (int j = i; j >= 0 && i - j < row; --j)
{
System.out.print("  " + data[i - j][j]);
}
}
// Second Half
for (int i = 1; i < row; ++i)
{
for (int j = col - 1, k = i; j >= 0 && k < row; --j, k++)
{
System.out.print("  " + data[k][j]);
}
}
}
public static void main(String[] args)
{
// 2D matrix
int [][]matrix = {
{1,  2,  4,  7,  11},
{3,  5,  8,  12, 15},
{6,  9,  13, 16, 18},
{10, 14, 17, 19, 20}
};
}
}``````

#### Output

``  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20``
``````/*
C Program
+ Diagonal traversal of a matrix
*/
#include <stdio.h>

#define ROW 4
#define COL 5
void diagonal(int data[ROW][COL])
{

// First Half Elements
for (int i = 0; i < COL; ++i)
{
for (int j = i; j >= 0 && i - j < ROW; --j)
{
printf("%4d", data[i - j][j]);
}
}
// Second Half
for (int i = 1; i < ROW; ++i)
{
for (int j = COL - 1, k = i; j >= 0 && k < ROW; --j, k++)
{
printf("%4d", data[k][j]);
}
}
}
int main()
{
int arr[ROW][COL] = {
{1,  2,  4,  7,  11},
{3,  5,  8,  12, 15},
{6,  9,  13, 16, 18},
{10, 14, 17, 19, 20}
};
diagonal(arr);
return 0;
}``````

#### Output

``   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20``
``````// Include header file
#include <iostream>
#include <vector>

using namespace std;
// C++ program
// Diagonal traversal of a matrix
class DiagonalTraversal
{
public: void diagonalView(vector<vector<int>> data)
{
// Get the size
int row = data.size();
int col = data[0].size();
// First Half
for (int i = 0; i < col; ++i)
{
for (int j = i; j >= 0 && i - j < row; --j)
{
cout << "  " << data[i - j][j];
}
}
// Second Half
for (int i = 1; i < row; ++i)
{
for (int j = col - 1, k = i; j >= 0 && k < row; --j, k++)
{
cout << "  " << data[k][j];
}
}
}
};
int main()
{
// 2D matrix
vector<vector<int>> matrix {
{
1 , 2 , 4 , 7 , 11
} , {
3 , 5 , 8 , 12 , 15
} , {
6 , 9 , 13 , 16 , 18
} , {
10 , 14 , 17 , 19 , 20
}
};
return 0;
}``````

#### Output

``  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20``
``````package main
import "fmt"
// Go program
// Diagonal traversal of a matrix

func diagonalView(data[][] int) {
// Get the size
var row int = len(data)
var col int = len(data[0])
// First Half
for i := 0 ; i < col ; i++ {
for j := i ; j >= 0 && i - j < row ; j-- {
fmt.Print("  ", data[i - j][j])
}
}
// Second Half
for i := 1 ; i < row ; i++ {
for j, k := col - 1, i ; j >= 0 && k < row ; j, k = j-1,k+1 {
fmt.Print("  ", data[k][j])
}
}
}
func main() {

// 2D matrix
var matrix = [][] int {
{1,  2,  4,  7,  11},
{3,  5,  8,  12, 15},
{6,  9,  13, 16, 18},
{10, 14, 17, 19, 20},
}
diagonalView(matrix)
}``````

#### Output

``  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20``
``````// Include namespace system
using System;
// Csharp program
// Diagonal traversal of a matrix
public class DiagonalTraversal
{
public void diagonalView(int[,] data)
{
// Get the size
int row = data.GetLength(0);
int col = data.GetLength(1);
// First Half
for (int i = 0; i < col; ++i)
{
for (int j = i; j >= 0 && i - j < row; --j)
{
Console.Write("  " + data[i - j,j]);
}
}
// Second Half
for (int i = 1; i < row; ++i)
{
for (int j = col - 1, k = i; j >= 0 && k < row; --j, k++)
{
Console.Write("  " + data[k,j]);
}
}
}
public static void Main(String[] args)
{
// 2D matrix
int[,] matrix = {
{
1 , 2 , 4 , 7 , 11
},
{
3 , 5 , 8 , 12 , 15
},
{
6 , 9 , 13 , 16 , 18
},
{
10 , 14 , 17 , 19 , 20
}
};
}
}``````

#### Output

``  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20``
``````<?php
// Php program
// Diagonal traversal of a matrix
class DiagonalTraversal
{
public  function diagonalView(\$data)
{
// Get the size
\$row = count(\$data);
\$col = count(\$data[0]);
// First Half
for (\$i = 0; \$i < \$col; ++\$i)
{
for (\$j = \$i; \$j >= 0 && \$i - \$j < \$row; --\$j)
{
print_r("  ".strval(\$data[\$i - \$j][\$j]));
}
}
// Second Half
for (\$i = 1; \$i < \$row; ++\$i)
{
for (\$j = \$col - 1, \$k = \$i; \$j >= 0 && \$k < \$row; --\$j, \$k++)
{
print_r("  ".strval(\$data[\$k][\$j]));
}
}
}
public static
function main(\$args)
{
// 2D matrix
\$matrix = array(array(1, 2, 4, 7, 11), array(3, 5, 8, 12, 15), array(6, 9, 13, 16, 18), array(10, 14, 17, 19, 20));
}
}
DiagonalTraversal::main(array());``````

#### Output

``  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20``
``````// Node JS program
// Diagonal traversal of a matrix
class DiagonalTraversal
{
diagonalView(data)
{
// Get the size
var row = data.length;
var col = data[0].length;
// First Half
for (var i = 0; i < col; ++i)
{
for (var j = i; j >= 0 && i - j < row; --j)
{
process.stdout.write("  " + data[i - j][j]);
}
}
// Second Half
for (var i = 1; i < row; ++i)
{
for (var j = col - 1, k = i;
j >= 0 && k < row; --j, k++)
{
process.stdout.write("  " + data[k][j]);
}
}
}
}

function main()
{
// 2D matrix
var matrix = [
[1, 2, 4, 7, 11],
[3, 5, 8, 12, 15],
[6, 9, 13, 16, 18],
[10, 14, 17, 19, 20]
];
}
main();``````

#### Output

``  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20``
``````#  Python 3 program
#  Diagonal traversal of a matrix
class DiagonalTraversal :
def diagonalView(self, data) :
#  Get the size
row = len(data)
col = len(data[0])
i = 0
#  First Half
while (i < col) :
j = i
while (j >= 0 and i - j < row) :
print("  ", data[i - j][j], end = "")
j -= 1
i += 1

i = 1
#  Second Half
while (i < row) :
j = col - 1
k = i
while (j >= 0 and k < row) :
print("  ", data[k][j], end = "")
j -= 1
k += 1

i += 1

def main() :
#  2D matrix
matrix = [
[1, 2, 4, 7, 11],
[3, 5, 8, 12, 15],
[6, 9, 13, 16, 18],
[10, 14, 17, 19, 20]
]

if __name__ == "__main__": main()``````

#### Output

``   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20``
``````#  Ruby program
#  Diagonal traversal of a matrix
class DiagonalTraversal
def diagonalView(data)
#  Get the size
row = data.length
col = data[0].length
i = 0
#  First Half
while (i < col)
j = i
while (j >= 0 && i - j < row)
print("  ", data[i - j][j])
j -= 1
end

i += 1
end

i = 1
#  Second Half
while (i < row)
j = col - 1
k = i
while (j >= 0 && k < row)
print("  ", data[k][j])
j -= 1
k += 1
end

i += 1
end

end

end

def main()
#  2D matrix
matrix = [
[1, 2, 4, 7, 11],
[3, 5, 8, 12, 15],
[6, 9, 13, 16, 18],
[10, 14, 17, 19, 20]
]
end

main()``````

#### Output

``  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20``
``````// Scala program
// Diagonal traversal of a matrix
class DiagonalTraversal()
{
def diagonalView(data: Array[Array[Int]]): Unit = {
// Get the size
var row: Int = data.length;
var col: Int = data(0).length;
var i: Int = 0;
// First Half
while (i < col)
{
var j: Int = i;
while (j >= 0 && i - j < row)
{
print("  " + data(i - j)(j));
j -= 1;
}
i += 1;
}
i = 1;
// Second Half
while (i < row)
{
var j: Int = col - 1;
var k: Int = i;
while (j >= 0 && k < row)
{
print("  " + data(k)(j));
j -= 1;
k += 1;
}
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: DiagonalTraversal = new DiagonalTraversal();
// 2D matrix
var matrix: Array[Array[Int]] = Array(
Array(1, 2, 4, 7, 11),
Array(3, 5, 8, 12, 15),
Array(6, 9, 13, 16, 18),
Array(10, 14, 17, 19, 20)
);
}
}``````

#### Output

``  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20``
``````import Foundation;
// Swift 4 program
// Diagonal traversal of a matrix
class DiagonalTraversal
{
func diagonalView(_ data: [[Int]])
{
// Get the size
let row: Int = data.count;
let col: Int = data[0].count;
var i: Int = 0;
// First Half
while (i < col)
{
var j: Int = i;
while (j >= 0 && i - j < row)
{
print("  ", data[i - j][j], terminator: "");
j -= 1;
}
i += 1;
}
i = 1;
// Second Half
while (i < row)
{
var j: Int = col - 1;
var k: Int = i;
while (j >= 0 && k < row)
{
print(" ", data[k][j], terminator: "");
j -= 1;
k += 1;
}
i += 1;
}
}
}
func main()
{
// 2D matrix
let matrix: [
[Int]
] = [
[1, 2, 4, 7, 11],
[3, 5, 8, 12, 15],
[6, 9, 13, 16, 18],
[10, 14, 17, 19, 20]
];
}
main();``````

#### Output

``   1   2   3   4   5   6   7   8   9   10   11   12   13   14  15  16  17  18  19  20``
``````// Kotlin program
// Diagonal traversal of a matrix
class DiagonalTraversal
{
fun diagonalView(data: Array < Array < Int >> ): Unit
{
// Get the size
val row: Int = data.count();
val col: Int = data[0].count();
var i: Int = 0;
// First Half
while (i < col)
{
var j: Int = i;
while (j >= 0 && i - j < row)
{
print("  " + data[i - j][j]);
j -= 1;
}
i += 1;
}
i = 1;
// Second Half
while (i < row)
{
var j: Int = col - 1;
var k = i;
while (j >= 0 && k < row)
{
print("  " + data[k][j]);
j -= 1;
k += 1;
}
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
// 2D matrix
val matrix: Array < Array < Int >> = arrayOf(arrayOf(1, 2, 4, 7, 11), arrayOf(3, 5, 8, 12, 15), arrayOf(6, 9, 13, 16, 18), arrayOf(10, 14, 17, 19, 20));
}``````

#### Output

``  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20``

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.