-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay4.java
51 lines (39 loc) · 1.13 KB
/
Day4.java
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
import java.util.*;
public class Day4 {
public static int diagonalDifference(List<List<Integer>> arr) {
// Write your code here
int rtl=0;
int ltr=0;
int size = arr.size();
for (int i = 0; i < size; i++) {
rtl += arr.get(i).get(i); // right-to-left diagonal
ltr += arr.get(i).get(size - 1 - i); // left-to-right diagonal
}
return Math.abs(rtl-ltr);
}
public static void main(String[] args) {
Day4 day4 = new Day4();
List<List<Integer>> arr = new ArrayList<>();
// Create the first row
List<Integer> row1 = new ArrayList<>();
row1.add(11);
row1.add(2);
row1.add(4);
arr.add(row1);
// Create the second row
List<Integer> row2 = new ArrayList<>();
row2.add(4);
row2.add(5);
row2.add(6);
arr.add(row2);
// Create the third row
List<Integer> row3 = new ArrayList<>();
row3.add(10);
row3.add(8);
row3.add(-12);
arr.add(row3);
//calling the method
int difference = day4.diagonalDifference(arr);
System.out.println("The absolute difference between the sums of the matrix's two diagonals is : "+difference);
}
}