diff --git a/algorithm-exercises-java/src/main/java/ae/hackerrank/DiagonalDifference.java b/algorithm-exercises-java/src/main/java/ae/hackerrank/DiagonalDifference.java new file mode 100644 index 0000000..b1a15b9 --- /dev/null +++ b/algorithm-exercises-java/src/main/java/ae/hackerrank/DiagonalDifference.java @@ -0,0 +1,31 @@ +package ae.hackerrank; + +import java.util.List; + +/** + * Diagonal Difference. + * + * @link Problem definition [[docs/hackerrank/warmup/diagonal_difference.md]] + */ +public class DiagonalDifference { + + private DiagonalDifference() {} + + static java.util.logging.Logger logger = util.CustomLogger.getLogger(); + + /** + * diagonalDifference. + */ + public static int diagonalDifference(List> matrix) { + int diag1 = 0; + int diag2 = 0; + int last = matrix.size() - 1; + + for (int i = 0; i < matrix.size(); i++) { + diag1 += matrix.get(i).get(i); + diag2 += matrix.get(last - i).get(i); + } + + return Math.abs(diag1 - diag2); + } +} diff --git a/algorithm-exercises-java/src/test/java/ae/hackerrank/DiagonalDifferenceTest.java b/algorithm-exercises-java/src/test/java/ae/hackerrank/DiagonalDifferenceTest.java new file mode 100644 index 0000000..9297b98 --- /dev/null +++ b/algorithm-exercises-java/src/test/java/ae/hackerrank/DiagonalDifferenceTest.java @@ -0,0 +1,26 @@ +package ae.hackerrank; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Test; + + +class DiagonalDifferenceTest { + + @Test void testDiagonalDifference() { + + List> matrix = Arrays.asList( + Arrays.asList(11, 2, 4), + Arrays.asList(4, 5, 6), + Arrays.asList(10, 8, -12) + ); + Integer expected = 15; + Integer resultFound = DiagonalDifference.diagonalDifference(matrix); + + assertEquals(expected, resultFound, + String.format("DiagonalDifference.diagonalDifference answer must be: %d", expected) + ); + } +} 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 $