Skip to content

Commit b57b0ea

Browse files
authored
Merge pull request #162 from sir-gon/feature/diagonal_difference
[Hacker Rank] Warm up: Diagonal Difference solved ✓
2 parents 42fee11 + 3a19acd commit b57b0ea

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ae.hackerrank;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Diagonal Difference.
7+
*
8+
* @link Problem definition [[docs/hackerrank/warmup/diagonal_difference.md]]
9+
*/
10+
public class DiagonalDifference {
11+
12+
private DiagonalDifference() {}
13+
14+
static java.util.logging.Logger logger = util.CustomLogger.getLogger();
15+
16+
/**
17+
* diagonalDifference.
18+
*/
19+
public static int diagonalDifference(List<List<Integer>> matrix) {
20+
int diag1 = 0;
21+
int diag2 = 0;
22+
int last = matrix.size() - 1;
23+
24+
for (int i = 0; i < matrix.size(); i++) {
25+
diag1 += matrix.get(i).get(i);
26+
diag2 += matrix.get(last - i).get(i);
27+
}
28+
29+
return Math.abs(diag1 - diag2);
30+
}
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ae.hackerrank;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.jupiter.api.Test;
8+
9+
10+
class DiagonalDifferenceTest {
11+
12+
@Test void testDiagonalDifference() {
13+
14+
List<List<Integer>> matrix = Arrays.asList(
15+
Arrays.asList(11, 2, 4),
16+
Arrays.asList(4, 5, 6),
17+
Arrays.asList(10, 8, -12)
18+
);
19+
Integer expected = 15;
20+
Integer resultFound = DiagonalDifference.diagonalDifference(matrix);
21+
22+
assertEquals(expected, resultFound,
23+
String.format("DiagonalDifference.diagonalDifference answer must be: %d", expected)
24+
);
25+
}
26+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# [Diagonal Difference](https://www.hackerrank.com/challenges/diagonal-difference)
2+
3+
Difficulty: #easy
4+
Category: #warmup
5+
6+
Given a square matrix, calculate the absolute difference between the sums
7+
of its diagonals.
8+
For example, the square matrix $ arr $ is shown below:
9+
10+
```text
11+
1 2 3
12+
4 5 6
13+
9 8 9
14+
```
15+
16+
The left-to-right $ diagonal = 1 + 5 + 9 = 15 $.
17+
The right to left $ diagonal = 3 + 5 + 9 = 17 $.
18+
Their absolute difference is $ |15 - 17| = 2 $.
19+
20+
## Function description
21+
22+
Complete the $ diagonalDifference $ function in the editor below.
23+
diagonalDifference takes the following parameter:
24+
25+
- int ` arr[n][m] `: an array of integers
26+
27+
## Return
28+
29+
- int: the absolute diagonal difference
30+
31+
## Input Format
32+
33+
The first line contains a single integer, n, the number of
34+
rows and columns in the square matrix arr.
35+
Each of the next n lines describes a row, arr[i], and consists of
36+
space-separated integers ` arr[i][j] `.
37+
38+
## Constraints
39+
40+
$ -100 \leq $ ` arr[i][j] ` $ \leq 100 $
41+
42+
## Output Format
43+
44+
Return the absolute difference between the sums of the matrix's
45+
two diagonals as a single integer.
46+
47+
## Sample Input
48+
49+
```text
50+
3
51+
11 2 4
52+
4 5 6
53+
10 8 -12
54+
```
55+
56+
Sample Output
57+
58+
```text
59+
15
60+
```
61+
62+
## Explanation
63+
64+
The primary diagonal is:
65+
66+
```text
67+
11
68+
5
69+
-12
70+
```
71+
72+
Sum across the primary diagonal: 11 + 5 - 12 = 4
73+
The secondary diagonal is:
74+
75+
```text
76+
4
77+
5
78+
10
79+
```
80+
81+
Sum across the secondary diagonal: $ 4 + 5 + 10 = 19 $
82+
Difference: $ |4 - 19| = 15 $
83+
84+
*Note*: $ |x| $ is the
85+
[absolute value](https://www.mathsisfun.com/numbers/absolute-value.html)
86+
of $ x $

0 commit comments

Comments
 (0)