forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSolution.java
33 lines (26 loc) · 1.05 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//Problem: https://www.hackerrank.com/challenges/diagonal-difference
//Java 8
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int leftDiagonal = input.nextInt(); //Handles first edge case of the element at matrix pos 0
int rightDiagonal = 0;
for(int i = 1; i<n*n; i++) //Uses modulus to only sum diagonal elements
{
int value = input.nextInt();
if(i%(n+1) == 0)
{
leftDiagonal += value;
}
if(i%(n-1) == 0 && i != (n*n)-1)//Handles the second edge case of the last element in the matrix
{
rightDiagonal += value;
}
}
System.out.println(Math.abs(leftDiagonal-rightDiagonal));
}
}