diff --git a/docs/hackerrank/warmup/diagonal_difference.md b/docs/hackerrank/warmup/diagonal_difference.md new file mode 100644 index 0000000..496e3a7 --- /dev/null +++ b/docs/hackerrank/warmup/diagonal_difference.md @@ -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 $ diff --git a/src/lib/exercises/include/exercises/hackerrank/warmup/diagonal_difference.h b/src/lib/exercises/include/exercises/hackerrank/warmup/diagonal_difference.h new file mode 100644 index 0000000..6610141 --- /dev/null +++ b/src/lib/exercises/include/exercises/hackerrank/warmup/diagonal_difference.h @@ -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 diff --git a/src/lib/exercises/src/hackerrank/warmup/diagonal_difference.c b/src/lib/exercises/src/hackerrank/warmup/diagonal_difference.c new file mode 100644 index 0000000..cb8f453 --- /dev/null +++ b/src/lib/exercises/src/hackerrank/warmup/diagonal_difference.c @@ -0,0 +1,24 @@ +#include + +/** + * @link Problem definition [[docs/hackerrank/warmup/diagonal_difference.md]] + */ + +#include + +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); +} diff --git a/src/tests/unit/lib/hackerrank/warmup/diagonal_difference.test.cpp b/src/tests/unit/lib/hackerrank/warmup/diagonal_difference.test.cpp new file mode 100644 index 0000000..93e4c58 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/diagonal_difference.test.cpp @@ -0,0 +1,65 @@ +#include + +#include +#include +#include + +#include +#include +#include +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); + } +} diff --git a/src/tests/unit/lib/hackerrank/warmup/diagonal_difference.testcases.json b/src/tests/unit/lib/hackerrank/warmup/diagonal_difference.testcases.json new file mode 100644 index 0000000..ea2b143 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/warmup/diagonal_difference.testcases.json @@ -0,0 +1,10 @@ +[ + { "matrix": + [ + [11, 2, 4], + [4, 5, 6], + [10, 8, -12] + ], + "expected": 15 + } +]