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
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int i, j;
int arr[i][j];
int x1 = 0, x2 = 0;
for (i = 1; i <= n; ++i)
{
for (j = 1; j <= n; ++j)
{
cin >> arr[i][j];
}
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (i == j)
x1 = x1 + arr[i][j];
}
}
for (i = 1; i <= n; i++)
{
for (j = n; j <= 1; j--)
{
x2 = x2 + arr[i][j];
}
}
cout << abs(x1 - x2);
}