Skip to content

Instantly share code, notes, and snippets.

@samarthsewlani
Created July 2, 2024 13:40
Show Gist options
  • Save samarthsewlani/eebf7a11ceb9fb4cb4598f09ca5dbbc2 to your computer and use it in GitHub Desktop.
Save samarthsewlani/eebf7a11ceb9fb4cb4598f09ca5dbbc2 to your computer and use it in GitHub Desktop.
Diagonal Difference HackerRank Solution
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