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 $
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#ifdef __cplusplus
extern "C" {
#endif

int HACKERRANK_WARMUP_diagonalDifference(int arr_rows, int arr_columns,
int **arr);

#ifdef __cplusplus
} // extern "C"
#endif
24 changes: 24 additions & 0 deletions src/lib/exercises/src/hackerrank/warmup/diagonal_difference.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <exercises/hackerrank/warmup/diagonal_difference.h>

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

#include <stdlib.h>

int HACKERRANK_WARMUP_diagonalDifference(int arr_rows, int arr_columns,
int **arr) {
int diag1 = 0;
int diag2 = 0;
int last = arr_rows - 1L;

for (int i = 0; i < arr_rows; i++) {
for (int j = 0; j < arr_columns; j++) {
if (i == j) {
diag1 += arr[i][j];
diag2 += arr[last - i][j];
}
}
}
return abs(diag1 - diag2);
}
65 changes: 65 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,65 @@
#include <catch2/catch_test_macros.hpp>

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

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

int **jsonToMatrix(const nlohmann::json &matrixJson, int &rows, int &cols) {
// Extraer dimensiones
rows = matrixJson.size();
cols = rows > 0 ? matrixJson[0].size() : 0;

// Crear matriz dinámica
int **matrix = new int *[rows];
for (int i = 0; i < rows; ++i) {
matrix[i] = new int[cols];
for (int j = 0; j < cols; ++j) {
matrix[i][j] = matrixJson[i][j];
}
}

return matrix;
}

void freeMatrix(int **matrix, int rows) {
for (int i = 0; i < rows; ++i) {
delete[] matrix[i];
}
delete[] matrix;
}

TEST_CASE("diagonalDifference JSON Test Cases",
"[hackerrank] [jsontestcase] [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);
nlohmann::json testCases = nlohmann::json::parse(f);

// Iterar sobre los casos de prueba
for (const auto &testCase : testCases) {
const auto &matrixJson = testCase["matrix"];
int expected = testCase["expected"];

int rows, cols;
int **matrix = jsonToMatrix(matrixJson, rows, cols);

// Llamar a la función bajo prueba
int result = HACKERRANK_WARMUP_diagonalDifference(rows, cols, matrix);

// Validar el resultado
REQUIRE(result == expected);

// Liberar memoria de la matriz
freeMatrix(matrix, rows);
}
}
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
}
]