Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions docs/hackerrank/warmup/diagonal_difference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# [Diagonal Difference](https://www.hackerrank.com/challenges/diagonal-difference)

Difficulty: #easy
Category: #warmup

Given a square matrix, calculate the absolute difference between the sums
of its diagonals.
For example, the square matrix $ arr $ is shown below:

```text
1 2 3
4 5 6
9 8 9
```

The left-to-right $ diagonal = 1 + 5 + 9 = 15 $.
The right to left $ diagonal = 3 + 5 + 9 = 17 $.
Their absolute difference is $ |15 - 17| = 2 $.

## Function description

Complete the $ diagonalDifference $ function in the editor below.
diagonalDifference takes the following parameter:

- int ` arr[n][m] `: an array of integers

## Return

- int: the absolute diagonal difference

## Input Format

The first line contains a single integer, n, the number of
rows and columns in the square matrix arr.
Each of the next n lines describes a row, arr[i], and consists of
space-separated integers ` arr[i][j] `.

## Constraints

$ -100 \leq $ ` arr[i][j] ` $ \leq 100 $

## Output Format

Return the absolute difference between the sums of the matrix's
two diagonals as a single integer.

## Sample Input

```text
3
11 2 4
4 5 6
10 8 -12
```

Sample Output

```text
15
```

## Explanation

The primary diagonal is:

```text
11
5
-12
```

Sum across the primary diagonal: 11 + 5 - 12 = 4
The secondary diagonal is:

```text
4
5
10
```

Sum across the secondary diagonal: $ 4 + 5 + 10 = 19 $
Difference: $ |4 - 19| = 15 $

*Note*: $ |x| $ is the
[absolute value](https://www.mathsisfun.com/numbers/absolute-value.html)
of $ x $
19 changes: 19 additions & 0 deletions src/hackerrank/warmup/diagonal_difference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// @link Problem definition [[docs/hackerrank/warmup/diagonal_difference.md]]

pub fn diagonal_difference(arr: &[Vec<i32>]) -> i32 {
let mut diag1 = 0;
let mut diag2 = 0;

let last = arr.len() - 1;
for (i, row) in arr.iter().enumerate() {
for(j, &value) in row.iter().enumerate() {
if i == j {
diag1 += value;
diag2 += arr[last - i][j];
}
}
}

(diag1 - diag2).abs()
}

1 change: 1 addition & 0 deletions src/hackerrank/warmup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod solve_me_first;
pub mod simple_array_sum;
pub mod compare_triplets;
pub mod a_very_big_sum;
pub mod diagonal_difference;
10 changes: 10 additions & 0 deletions tests/data/hackerrank/warmup/diagonal_difference.testcases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{ "matrix":
[
[11, 2, 4],
[4, 5, 6],
[10, 8, -12]
],
"expected": 15
}
]
31 changes: 31 additions & 0 deletions tests/hackerrank/warmup/diagonal_difference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use exercises::hackerrank::warmup::diagonal_difference::diagonal_difference;
use once_cell::sync::Lazy;
use serde::Deserialize;

use crate::common;
use common::utils::load_json;

#[cfg(test)]
mod tests {
use super::*;

#[derive(Debug, Deserialize)]
struct DiagonalDifferenceTestCase {
matrix: Vec<Vec<i32>>,
expected: i32
}

static TEST_DATA: Lazy<Vec<DiagonalDifferenceTestCase>> =
Lazy::new(|| load_json("tests/data/hackerrank/warmup/diagonal_difference.testcases.json"));

#[test]
fn test_diagonal_difference() {
println!("Testing hackerrank::warmup::diagonal_difference::diagonal_difference()");

for test_case in TEST_DATA.iter() {
let slice: &[Vec<i32>] = &test_case.matrix;
let result = diagonal_difference(slice);
assert_eq!(result, test_case.expected);
}
}
}
1 change: 1 addition & 0 deletions tests/hackerrank/warmup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod solve_me_first;
pub mod simple_array_sum;
pub mod compare_triplets;
pub mod a_very_big_sum;
pub mod diagonal_difference;
Loading