-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagonalDifference.cpp
More file actions
48 lines (44 loc) · 1.15 KB
/
diagonalDifference.cpp
File metadata and controls
48 lines (44 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
For example, the square matrix is shown below:
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.
*/
#include <iostream>
using namespace std;
#include <vector>
int diagonalDifference(vector<vector<int>> arr)
{
int sum1 = 0, sum2 = 0, result, n = arr.size();
for (int i = 0; i < n; i++)
{
sum1 += arr[i][i];
sum2 += arr[n - i - 1][i];
}
result = sum2 - sum1;
return (result >= 0) ? result : -result;
}
int main()
{
int n, val;
cout << "Enter dimension of the square matrix: ";
cin >> n;
vector<vector<int>> arr(n);
cout << "Enter " << n * n << " values:\n";
for (int i = 0; i < n; i++)
{
arr[i].resize(n);
for (int j = 0; j < n; j++)
{
cout << "Value [" << i << "][" << j << "]: ";
cin >> val;
arr[i][j] = val;
}
}
cout << "The diagonal difference is: " << diagonalDifference(arr) << endl;
return 0;
}