Skip to content

[Hacker Rank] Warm up: Diagonal Difference solved ✓ #162

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
May 24, 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
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> 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)
);
}
}
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 $
Loading