Posts Anti Diagonals (InterviewBit)
Post
Cancel

Anti Diagonals (InterviewBit)

PROBLEM DESCRIPTION

Give a N x N square matrix, return an array of its anti-diagonals. Look at the example for more details.

Input:

1
2
3
1 2 3
4 5 6
7 8 9

Output:

1
2
3
4
5
6
7
[
  [1],
  [2, 4],
  [3, 5, 7],
  [6, 8],
  [9]
]

InterviewBit

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class Solution {

    public ArrayList<ArrayList<Integer>> diagonal(ArrayList<ArrayList<Integer>> A) {

        int n = A.size();

        ArrayList<ArrayList<Integer>> diagonals = new ArrayList<>();

        // Diagonals starting from row 0
        for(int i = 0; i < n; i++){

            int r = 0;  // Start from the first row
            int c = i;  // The current diagonal will start from column at index i

            ArrayList<Integer> diag = new ArrayList<>();

            // The next position will be A[r++][c--]
            while(c >= 0 && r < n){
                diag.add(A.get(r).get(c));
                r++;
                c--;
            }

            diagonals.add(diag);

        }

        // Diagonals starting from last column (except topmost)
        // The diagonal starting from top right corner was already covered in the previous loop
        for(int i = 1; i < n; i++){

            int r = i;  // The diagonal will start from row at index i
            int c = n - 1;  // The diagonal will start from the last column

            ArrayList<Integer> diag = new ArrayList<>();

            // // The next position will be A[r++][c--]
            while(c >= 0 && r < n){
                diag.add(A.get(r).get(c));
                r++;
                c--;
            }

            diagonals.add(diag);

        }

        return diagonals;

    }
}
This post is licensed under CC BY 4.0 by the author.