Created
February 27, 2018 09:12
-
-
Save cjae/c11f2b0779f21c631b5c3a791e88d206 to your computer and use it in GitHub Desktop.
Solution to Hackerrank Diagonal Difference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
import java.text.*; | |
import java.math.*; | |
import java.util.regex.*; | |
public class Solution { | |
static int diagonalDifference(int[][] a) { | |
// Complete this function | |
int col = 0; | |
int row = a.length - 1; | |
int total1 = 0; | |
int total2 = 0; | |
for (int i = 0; i < a.length; i++) { | |
total1 += a[i][i]; | |
total2 += a[col][row]; | |
col++; | |
row--; | |
} | |
return Math.abs(total1 - total2); | |
} | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int n = in.nextInt(); | |
int[][] a = new int[n][n]; | |
for(int a_i = 0; a_i < n; a_i++){ | |
for(int a_j = 0; a_j < n; a_j++){ | |
a[a_i][a_j] = in.nextInt(); | |
} | |
} | |
int result = diagonalDifference(a); | |
System.out.println(result); | |
in.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment