Skip to content

Commit af68a4a

Browse files
authored
Merge pull request #26 from sir-gon/feature/diagonal_difference
[Hacker Rank]: Warmup: Diagonal Difference. Solved ✅.
2 parents da28d3f + 7690516 commit af68a4a

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed
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 $
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @link Problem definition [[docs/hackerrank/warmup/diagonal_difference.md]]
2+
3+
pub fn diagonal_difference(arr: &[Vec<i32>]) -> i32 {
4+
let mut diag1 = 0;
5+
let mut diag2 = 0;
6+
7+
let last = arr.len() - 1;
8+
for (i, row) in arr.iter().enumerate() {
9+
for(j, &value) in row.iter().enumerate() {
10+
if i == j {
11+
diag1 += value;
12+
diag2 += arr[last - i][j];
13+
}
14+
}
15+
}
16+
17+
(diag1 - diag2).abs()
18+
}
19+

src/hackerrank/warmup/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pub mod solve_me_first;
22
pub mod simple_array_sum;
33
pub mod compare_triplets;
44
pub mod a_very_big_sum;
5+
pub mod diagonal_difference;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{ "matrix":
3+
[
4+
[11, 2, 4],
5+
[4, 5, 6],
6+
[10, 8, -12]
7+
],
8+
"expected": 15
9+
}
10+
]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use exercises::hackerrank::warmup::diagonal_difference::diagonal_difference;
2+
use once_cell::sync::Lazy;
3+
use serde::Deserialize;
4+
5+
use crate::common;
6+
use common::utils::load_json;
7+
8+
#[cfg(test)]
9+
mod tests {
10+
use super::*;
11+
12+
#[derive(Debug, Deserialize)]
13+
struct DiagonalDifferenceTestCase {
14+
matrix: Vec<Vec<i32>>,
15+
expected: i32
16+
}
17+
18+
static TEST_DATA: Lazy<Vec<DiagonalDifferenceTestCase>> =
19+
Lazy::new(|| load_json("tests/data/hackerrank/warmup/diagonal_difference.testcases.json"));
20+
21+
#[test]
22+
fn test_diagonal_difference() {
23+
println!("Testing hackerrank::warmup::diagonal_difference::diagonal_difference()");
24+
25+
for test_case in TEST_DATA.iter() {
26+
let slice: &[Vec<i32>] = &test_case.matrix;
27+
let result = diagonal_difference(slice);
28+
assert_eq!(result, test_case.expected);
29+
}
30+
}
31+
}

tests/hackerrank/warmup/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pub mod solve_me_first;
22
pub mod simple_array_sum;
33
pub mod compare_triplets;
44
pub mod a_very_big_sum;
5+
pub mod diagonal_difference;

0 commit comments

Comments
 (0)