In this article, We are providing Diagonal Difference Hackerrank Solution in C, C++, and Java programming Languages. This programming problem belongs to hackerrank 30 days of code, and we are going to find the Hackerrank Diagonal Difference Solution in C. We have to find the Diagonal Difference of an **N*N** matrix or a square matrix. We have to find the right diagonal sum of a matrix and the left diagonal sum of the matrix, then find out the hackerrank diagonal difference of a matrix. In this matrix, we already know that our matrix size is an **N*N.**

## Diagonal Difference Hackerrank

- Function Description
- Input Format
- Constraints
- Output Format
- Sample Input
- Sample Output
- Explanation of Diagonal Difference Hackerrank
- The Logic of the Diagonal Difference Hackerrank
- Diagonal Difference Hackerrank Solution in C
- Diagonal Difference Hackerrank Solution in C++

Given a square matrix, calculate the absolute difference between the sums of its Diagonal Difference Hackerrank Solution.

1 2 3

4 5 6

9 8 9

The left-to-right diagonal =

**1 + 5 + 9 = 15**. The right to left diagonal =

**3 + 5 + 9 = 17**. Their absolute difference is

**|15-17| = 2**.

### Function Description

Complete the

**DiagonalDifference**function in the editor below. It must return an integer representing the absolute diagonal difference.

diagonalDifference takes the following parameter:

**arr:**an array of integers.

### Input Format

The first line contains a single integer, n, the number of rows and columns in the matrix arr. Each of the next n lines describes a row, arr[i], and consists of n space-separated integers arr[i][j].

### Constraints

**-100<=arr[i][j]<=100**

### Output Format

Print the absolute difference between the sums of the matrix's two diagonals as a single integer.

### Sample Input

3

11 2 4

4 5 6

10 8 -12

### Sample Output

15

### Explanation of Diagonal Difference Hackerrank Sample

The primary diagonal is:

Sum across the primary diagonal: 11 + 5 - 12 = 4

The secondary diagonal is

Sum across the secondary diagonal: 4 + 5 + 10 = 19

Difference: |4 - 19| = 15

Now the last step is to find the difference between the sum of diagonals, so add the first diagonal and the second diagonal after that mod the difference so | 4 - 19| = 15. Hence we got our solution.

**Note:**|x| is the absolute value of x

**Check This-**Hackerrank 30 days of code Solution.

### Diagonal Difference Hackerrank Logic

Submit your solution here:-Click here

## Diagonal Difference Hackerrank Solution in C

```
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main()
{
int n, j;
int i=0,RightDiagonalSum=0,LeftDiagonalSum=0, firstarray, secondarray;
scanf("%d",&n);
int a[n][n];
for(int firstarray = 0; firstarray < n; firstarray++)
{
for(int secondarray = 0; secondarray < n; secondarray++)
{
scanf("%d",&a[firstarray][secondarray]);
}
}
while(i<n)
{
RightDiagonalSum=RightDiagonalSum+a[i][i];
i++;
}
j=n-1,i=0;
while(i<n)
{
LeftDiagonalSum=LeftDiagonalSum+a[i][j];
i++;
j--;
}
printf("%d",abs(RightDiagonalSum-LeftDiagonalSum));
return 0;
}
```

### Diagonal Difference Hackerrank Output

## Diagonal Difference Hackerrank Solution in C++

```
#include <iostream>
using namespace std;
int main()
{
int N, LeftDiagonalSum = 0, RightDiagonal = 0;
cin >> N;
int a[N][N];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cin >> a[i][j];
if (i == j)
{
LeftDiagonalSum = LeftDiagonalSum + a[i][j];
}
}
}
for (int i = 0; i < N; i++)
{
for (int j = N - 1 - i; j >= 0;)
{
RightDiagonal = RightDiagonal + a[i][j];
break;
}
}
cout << abs(LeftDiagonalSum - RightDiagonal) << endl;
return 0;
}
```

## 0 Comments: