Objective:
This diagonal difference problem involves basic matrix traversal and arithmetic operations to calculate the required difference. Calculate the absolute difference between the sums of the matrix’s two diagonals.
Problem Statement:
You are given a square matrix of integers. Your task is to calculate the absolute difference between the sums of its primary (top-left to bottom-right) and secondary (top-right to bottom-left) diagonals.
Input Format:
- The first line contains a single integer
n
, the number of rows and columns in the matrix. - Each of the next
n
lines containn
space-separated integers representing the elements of the matrix.
Output Format:
- A single integer represents the absolute difference between the sums of the primary and secondary diagonals.
Example 1:
- Input:
n = 3
matrix =
1 2 4
4 5 6
10 8 -12
- Output:
15
- Explanation: The primary diagonal is 11, 5, -12, and the secondary diagonal is 4, 5, 10.
- Primary diagonal sum: 11 + 5 – 12 = 4
- Secondary diagonal sum: 4 + 5 + 10 = 19
- Absolute difference: |4 – 19| = 15
Example 2:
- Input:
n = 2
matrix =
1 2
3 4
- Output:
0
- Explanation: The primary diagonal is 1, 4, and the secondary diagonal is 2, 3.
- Primary diagonal sum: 1 + 4 = 5
- Secondary diagonal sum: 2 + 3 = 5
- Absolute difference: |5 – 5| = 0
Example 3:
- Input:
n = 4
matrix =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
- Output:
0
- Explanation: The primary diagonal is 1, 6, 11, 16, and the secondary diagonal is 4, 7, 10, 13.
- Primary diagonal sum: 1 + 6 + 11 + 16 = 34
- Secondary diagonal sum: 4 + 7 + 10 + 13 = 34
- Absolute difference: |34 – 34| = 0
Steps to Solve:
- Initialize Sums: Start with sums for the primary and secondary diagonals set to 0.
- Traverse the Matrix: Loop through the matrix to add up the elements of the primary and secondary diagonals.
- The primary diagonal elements are at the indices
(i, i)
. - The secondary diagonal elements are at the indices
(i, n-1-i)
.
- The primary diagonal elements are at the indices
- Calculate Absolute Difference: Compute the absolute difference between the two sums.
- Output the Result: Print the absolute difference.
Diagonal Difference Problem Snippet:
Diagonal Difference Solution:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'diagonalDifference' function below.
*
* The function is should return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
*/
public static int diagonalDifference(List<List<Integer>> arr) {
// Write your code here
int diagonal1 = 0;
int diagonal2 = 0;
int length = arr.get(0).size() - 1;
for(int i=0; i<= length; i++){
diagonal1 += arr.get(i).get(i);
diagonal2 += arr.get(i).get(length-i);
}
return Math.abs(diagonal1 - diagonal2);
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<List<Integer>> arr = new ArrayList<>();
IntStream.range(0, n).forEach(i -> {
try {
arr.add(
Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList())
);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
int result = Result.diagonalDifference(arr);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}