-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagonalDiffrence.cpp
More file actions
56 lines (46 loc) · 1.16 KB
/
diagonalDiffrence.cpp
File metadata and controls
56 lines (46 loc) · 1.16 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
49
50
51
52
53
54
55
56
#include <vector>
using namespace std;
int diagonalDifference(vector<vector<int>> arr) {
int sumOfPrimaryDiagonal = 0;
int sumOfSecondaryDiagonal = 0;
int sum;
for (int i = 0; i < arr.size(); i++) {
sumOfPrimaryDiagonal += arr[i][i];
}
for (int i = 0; i < arr.size(); i++) {
sumOfSecondaryDiagonal += arr[i][arr.size() - i - 1];
}
if (sumOfSecondaryDiagonal < sumOfPrimaryDiagonal){
sum = sumOfPrimaryDiagonal - sumOfSecondaryDiagonal;
}else {
sum = sumOfSecondaryDiagonal - sumOfPrimaryDiagonal;
}
return sum;
}
//Info: Testcases Worked
//int main() {
// vector<vector<int>> array =
// {
// {1, 2, 3},
// {4, 5, 6},
// {7, 8, 9}
// };
// diagonalDifference(array);
// return 0;
//}
/*
1 2 3
4 5 6
9 8 9
*/
/*
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 = 2 + 5 + 9 = 17.
Their absolute difference is
[15 - 17] = 2.
*/