+ 1

Challenge Diagonal Difference

Given a square matrix of NxN size , calculate the absolute difference between the sums of its diagonals. Input Format The first line contains a single integer,N . The next N lines denote the matrix's rows, with each line containing space-separated integers describing the columns. Constraints -100<= Elements in the matrix<=100 Output Format Print the absolute difference between the two sums of the matrix's diagonals as a single integer. 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 Note: |x| is absolute value function

2nd Jan 2018, 7:27 AM
Sivaraj Sivasubramaniam
Sivaraj Sivasubramaniam - avatar
2 odpowiedzi
0
Assume the matrix as mat in C++: int mdsum=0, mat[50][50], n, m; for(int*& i : mat) { for(int& j : i) cin>>i; } for(int i=0;i<n;i++) { mdsum+=abs(mat[i][i]-mat[i][n-i-1]); } cout<<mdsum<<endl;
2nd Jan 2018, 2:02 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar