Created
July 2, 2024 13:40
-
-
Save samarthsewlani/eebf7a11ceb9fb4cb4598f09ca5dbbc2 to your computer and use it in GitHub Desktop.
Diagonal Difference HackerRank Solution
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.util.*; | |
public class Solution { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int n = sc.nextInt(); | |
int matrix[][] = new int[n][n]; | |
for(int i=0;i<n;i++){ | |
for(int j=0;j<n;j++){ | |
matrix[i][j] = sc.nextInt(); | |
} | |
} | |
int diag1 = 0, diag2 = 0; | |
for(int i=0;i<n;i++){ | |
diag1+= matrix[i][i]; | |
int j = n-1-i; | |
diag2+= matrix[i][j]; | |
} | |
System.out.println(Math.abs(diag1-diag2)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment