Elements of Anti Diagonal
Write a program to print the anti-diagonal elements in the given matrix.
Input
The first line of input will contain an integer N, denoting the number of rows and columns of the input matrix.
The next N following lines will contain N space-separated integers, denoting the elements of each row of the matrix.
Output
The output should be a list containing the anti-diagonal elements.
Explanation
For example, if the given N is 3, and the given matrix is
1 2 3
4 5 6
7 8 9
The anti diagonal elements of the above matrix are 3, 5, 7. So the output should be the list
[3, 5, 7]
Sample Input 1
3
1 2 3
4 5 6
7 8 9
Sample Output 1
[3, 5, 7]
Sample Input 2
5
44 71 46 2 15
97 21 41 69 18
78 62 77 46 63
16 92 86 21 52
71 90 86 17 96
Sample Output 2
[15, 69, 77, 92, 71]
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given a square matrix of size N*N, return an array of its anti-diagonals. For better understanding let us look at the image given below:
Examples:
Input :
Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 16
Approach 1:
To solve the problem mentioned above we have two major observations.
- The first one is, some diagonals start from the zeroth row for each column and ends when either start column >= 0 or start row < N.
- While the second observation is that the remaining diagonals start with end column for each row and ends when either start row < N or start column >= 0.
Below is the implementation of the above approach:
C++
#include
using
namespace
std;
void
diagonal[
int
A[3][3]]
{
int
N = 3;
for
[
int
col = 0; col < N; col++] {
int
startcol = col, startrow = 0;
while
[startcol >= 0 && startrow < N] {
cout = 0 && startrow < N] {
Console.Write[A[startrow, startcol] +
" "
];
startcol--;
startrow++;
}
Console.WriteLine[];
}
for
[
int
row = 1; row < N; row++] {
int
startrow = row, startcol = N - 1;
while
[startrow < N && startcol >= 0] {
Console.Write[A[startrow, startcol] +
" "
];
startcol--;
startrow++;
}
Console.WriteLine[];
}
}
public
static
void
Main[
string
[] args]
{
int
[, ] A
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
diagonal[A];
}
}
Javascript
function
diagonal[A]
{
let N = 3;
for
[let col = 0; col < N; col++]
{
let startcol = col, startrow = 0;
while
[startcol >= 0 && startrow < N]
{
document.write[A[startrow][startcol] +
" "
];
startcol--;
startrow++;
}
document.write[
""
];
}
for
[let row = 1; row < N; row++]
{
let startrow = row, startcol = N - 1;
while
[startrow < N && startcol >= 0]
{
document.write[A[startrow][startcol] +
" "
];
startcol--;
startrow++;
}
document.write[
""
];
}
}
let A = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
diagonal[A];
Time Complexity: O[N*N], Where N is the number of rows or columns of given matrix.
Auxiliary Space:O[1]
Approach 2: Much simpler and concise [Same time Complexity]
In this approach, we will make the use of sum of indices of any element in a matrix. Let indices of any element be represented by i [row] an j [column].
If we find the sum of indices of any element in a N*N matrix, we will observe that the sum of indices for any element lies between 0 [when i = j = 0] and 2*N – 2 [when i = j = N-1].
So we will follow the following steps:
- Declare a vector of vectors of size 2*N – 1 for holding unique sums from sum = 0 to sum = 2*N – 2.
- Now we will loop through the vector and pushback the elements of similar sum to same row in that vector of vectors.
Below is the implementation of the above approach:
C++
#include
#include
using
namespace
std;
void
diagonal[vector& A]
{
int
n = A.size[];
int
N = 2 * n - 1;
vector result[N];
for
[
int
i = 0; i < n; i++]
for
[
int
j = 0; j < n; j++]
result[i + j].push_back[A[i][j]];
for
[
int
i = 0; i < result.size[]; i++]
{
cout