Skip to content

[Hacker Rank]: Warmup: Diagonal Difference. Solved ✅. #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2024
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 $
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <vector>

#pragma once

namespace hackerrank::warmup {
int diagonalDifference(const std::vector<std::vector<int>>& arr);
}
28 changes: 28 additions & 0 deletions src/lib/exercises/src/hackerrank/warmup/diagonal_difference.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <exercises/hackerrank/warmup/diagonal_difference.hpp>

/**
* @link Problem definition [[docs/hackerrank/warmup/diagonal_difference.md]]
*/

#include <numeric>
#include <vector>
#include <cstddef>
#include <cstdlib>

namespace hackerrank::warmup {
int diagonalDifference(const std::vector<std::vector<int>>& arr) {
int diag1 = 0;
int diag2 = 0;
size_t last = arr.size() - 1L;

for (int i = 0; i < arr.size(); i++) {
for (int j = 0; j < arr[i].size(); j++) {
if (i == j) {
diag1 += arr[i][j];
diag2 += arr[last - i][j];
}
}
}
return abs(diag1 - diag2);
}
}
26 changes: 26 additions & 0 deletions src/tests/unit/lib/hackerrank/warmup/diagonal_difference.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <catch2/catch_test_macros.hpp>

#include <exercises/hackerrank/warmup/diagonal_difference.hpp>
#include <iostream>
#include <vector>

#include <filesystem>
#include <fstream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;

TEST_CASE("diagonalDifference JSON Test Cases", "[warmup]")
{
std::filesystem::path cwd = std::filesystem::current_path();
std::string path = cwd.string() + "/unit/lib/hackerrank/warmup/diagonal_difference.testcases.json";

INFO("diagonalDifference JSON test cases FILE: " << path);

std::ifstream f(path);
json data = json::parse(f);

for (auto testcase : data) {
long result = hackerrank::warmup::diagonalDifference(testcase["matrix"]);
CHECK(result == testcase["expected"]);
}
}
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
}
]
Loading