Skip to content

Commit 4bb5d8e

Browse files
Merge pull request #67 from ayanujju/patch-1
Diagonal difference
2 parents 804ea06 + 0814062 commit 4bb5d8e

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

Diagonal-Difference.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
int main()
4+
{
5+
int n,i,j,sum1=0,sum2=0; //n denotes the number of rows and columns in the matrix arr.
6+
7+
scanf("%d", &n);
8+
int arr[n][n];
9+
for (i=0;i<n;i++)
10+
{
11+
for (j=0;j<n;j++)
12+
{
13+
scanf("%d ", &arr[i][j]);
14+
//Taking diagonal sum of the matrix arr from the both side
15+
if(arr[i][j]>=-100 && arr[i][j]<=100)
16+
{
17+
if(i==j)
18+
{
19+
sum1+=arr[i][j];
20+
}
21+
if(j==(n-1-i))
22+
{
23+
sum2+=arr[i][j];
24+
}
25+
}
26+
}
27+
}
28+
// This code part belongs to the absolute difference between the sums of the matrix's along two diagonals
29+
if((sum1-sum2)<0)
30+
{
31+
printf("%d", (-((sum1)-(sum2))));
32+
}
33+
else
34+
{
35+
printf("%d", ((sum1)-(sum2)));
36+
}
37+
return 0;
38+
}
39+
40+
/*
41+
Sample Input
42+
43+
3
44+
11 2 4
45+
4 5 6
46+
10 8 -12
47+
Sample Output
48+
49+
15
50+
51+
Explanation
52+
53+
The primary diagonal is:
54+
55+
11
56+
5
57+
-12
58+
Sum across the primary diagonal: 11 + 5 - 12 = 4
59+
60+
The secondary diagonal is:
61+
62+
4
63+
5
64+
10
65+
Sum across the secondary diagonal: 4 + 5 + 10 = 19
66+
Difference: |4 - 19| = 15
67+
*/

0 commit comments

Comments
 (0)