2D Matrix Absolute Difference Diagonals Elements Sum
A Square Matrix has N rows and N columns. Get the matrix as input and find the absolute difference between the sum of the values in the two diagonals.
Input Format:
The first line contains N.
Next N lines containing the elements of the matrix with N column values separated by a space.
Next N lines containing the elements of the matrix with N column values separated by a space.
Output Format:
Absolute difference between the sum of the values in the two diagonals.
Boundary Conditions:
1 <= N <= 100
1 <= N <= 100
Example Input/Output 1:
Input:
2
35 80
90 20
Input:
2
35 80
90 20
Output:
115
Example Input/Output 2:
Input:
4
1 4 4 2
2 2 1 8
8 1 3 5
10 1 8 3
Input:
4
1 4 4 2
2 2 1 8
8 1 3 5
10 1 8 3
Output:
5
5
Code:
#include <iostream>
#include<stdlib.h>
using namespace std;
int main(int argc, char** argv)
{
int
n,d=0,d1=0;
cin>>n;
int
arr[n][n],i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>arr[i][j];
if(i==j)
d+=arr[i][j];
if(i+j==n-1)
d1+=arr[i][j];
}
}
cout<<abs(d1-d);
}
#v2#v2lancer
#v2#v2lancer
No comments:
Post a Comment