In the diagonal difference challenge of hackerrank, the user is provided a square matrix of N*N size and the challenge is to calculate the absolute difference between the left to right diagonal and right to left diagonal.
The part where I got stuck was finding the sum of the second diagonal. The sum of the first diagonal was easy.
Also, I learned about the inbuilt absolute function of the language, this function will be used later to find the absolute difference between first and the second diagonal. Make sure to get the syntax of absolute function correct.
Diagonal Difference challenge
Sample input
3 11 2 4 4 5 6 10 8 -12
Sample Output
15
Explanation
The primary diagonal is:
11 5 -12
Sum across the primary diagonal: 11 + 5 – 12 = 4
The secondary diagonal is:
4 5 10
Sum across the secondary diagonal: 4 + 5 + 10 = 19
Difference: |4 – 19| = 15
Diagonal Difference HackerRank Solution in C
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int N , a[100][100]; scanf("%d",&N); int i,j,d1=0,d2=0; for(i=0;i<N;i++) for(j=0;j<N;j++) scanf("%d",&a[i][j]); for(i=0;i<N;i++) { d1+=a[i][i]; d2+=a[i][N-1-i]; } int b=abs(d1-d2); printf("%d",b); return 0; }
Diagonal Difference HackerRank Solution in C++
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int N; cin >> N; int i, j; int sumdiag1 = 0; int sumdiag2 = 0; for(i = 0; i < N; i++){ for(j = 0; j< N; j++) { int no; cin >> no; if(i == j) sumdiag1 += no; if(i+j == N-1) sumdiag2 += no; } } cout << abs(sumdiag1 - sumdiag2); return 0; }
Diagonal Difference HackerRank Solution in Java
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { int n; Scanner in=new Scanner(System.in); n=in.nextInt(); int a[][]=new int[n][n]; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ a[i][j]=in.nextInt(); } } int pd=0,npd=0; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(j==i) pd=pd+a[i][j]; } } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(i==n-j-1){ npd=npd+a[i][j]; } } } int dif=npd-pd; dif=Math.abs(dif); System.out.println(dif); } }
Diagonal Difference HackerRank Solution in Python
n = input() arr=[] for _ in range(n): temp = map(int,raw_input().split()) arr.append(temp) s1,s2 = 0,0 for i in range(n): s1 += arr[i][i] s2 += arr[-i-1][i] print abs(s1-s2)
Attempt Diagonal Difference HackerRank Challenge
Challenge link : https://www.hackerrank.com/challenges/diagonal-difference/
Next Challenge – Diagonal Difference HackerRank Solution
Link: https://exploringbits.com/plus-minus-hackerrank-solution/
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.