QUESTION : -
[N.B. - This question has been taken from DIAGONAL DIFFERENCE.]
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
For example, the square matrix arr 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
Input Format
The first line contains a single integer, n, the number of rows and columns in the matrix arr.
Each of the next n lines describes a row, , arr[i], and consists of n space-separated integers arr[i][j].
Constraints
-100 ≤ arr[i][j] ≤ 100
Output Format
Print the absolute difference between the sums of the matrix's two diagonals as a single integer.
Sample Input
3
11 2 4
4 5 6
10 8 -12
Sample Output
15
Ans:-
This question is nothing but a basic application of multidimensional array.
arr[i][j] lies on the first diagonal if i=j and it lies on the second diagonal if i = n - j -1. Just this trick is enough to calculate the sum of diagonal elements and their absolute difference.
C++ Code :-
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
int diag1=0,diag2=0;
cin >> n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int a[n][n];
cin >> a[i][j];
if(i==j)
{
diag1+=a[i][j];
}
if(i==(n-j-1))
{
diag2+=a[i][j];
}
}
}
int diff;
diff=diag1-diag2;
cout << abs(diff);
return 0;
}
Python Code :- (A little bit different approach)
size = int(input()) # taking input of the size from the user
difference = 0 # initializing the difference variable to 0
for i in range(0,size): # starting the outer loop
line = input().split() # splitting the user input to a list
for j in range(size): # starting the inner loop
if j==i:
left_dig = int(line[j])
if j==size-i-1:
right_dig = int(line[j])
difference += left_dig # adding the left diagonal value from the list
difference -= right_dig # subtracting the right diagonal value from the list
print(abs(difference)) # printing the absolute difference of both
Hope that helps, Keep Coding :)
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
For example, the square matrix arr 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
Input Format
The first line contains a single integer, n, the number of rows and columns in the matrix arr.
Each of the next n lines describes a row, , arr[i], and consists of n space-separated integers arr[i][j].
Constraints
Output Format
Print the absolute difference between the sums of the matrix's two diagonals as a single integer.
Sample Input
3
11 2 4
4 5 6
10 8 -12
Sample Output
15
Ans:-
This question is nothing but a basic application of multidimensional array.
arr[i][j] lies on the first diagonal if i=j and it lies on the second diagonal if i = n - j -1. Just this trick is enough to calculate the sum of diagonal elements and their absolute difference.
C++ Code :-
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
int diag1=0,diag2=0;
cin >> n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int a[n][n];
cin >> a[i][j];
if(i==j)
{
diag1+=a[i][j];
}
if(i==(n-j-1))
{
diag2+=a[i][j];
}
}
}
int diff;
diff=diag1-diag2;
cout << abs(diff);
return 0;
}
Python Code :- (A little bit different approach)
size = int(input()) # taking input of the size from the user
difference = 0 # initializing the difference variable to 0
for i in range(0,size): # starting the outer loop
line = input().split() # splitting the user input to a list
for j in range(size): # starting the inner loop
if j==i:
left_dig = int(line[j])
if j==size-i-1:
right_dig = int(line[j])
difference += left_dig # adding the left diagonal value from the list
difference -= right_dig # subtracting the right diagonal value from the list
print(abs(difference)) # printing the absolute difference of both
Hope that helps, Keep Coding :)
Comments
Post a Comment