Diagonal difference - Hackerrank solution in C & C++

 Problem : 

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 = . The right to left diagonal = . Their absolute difference is .

Function description

Complete the  function in the editor below.

diagonal  Difference takes the following parameter:

  • int arr[n][m]: an array of integers

Return

  • int: the absolute diagonal difference

Input Format

The first line contains a single integer, , the number of rows and columns in the square matrix .
Each of the next  lines describes a row, , and consists of  space-separated integers .

Constraints

Output Format

Return 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

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 the absolute value of x

Solution :  

Every square  matrix has two diagonals . E.g if a matrix is of order 3*3  like 

A11    A12   A13

A21    A22  A23

A31   A32   A33 

then first diagonal of the matrix will contain elements- A11,A22,A33  

and second diagonal will contain elements - A13  ,A22 , A31 . 

Here it is quite clear from the problem statment that we have to find the sum  of diagonal elements OF both diagnals and then find the difference between sum of those diagonal elements  i.e. in above matrix we have to find sum1= A11 + A22 + A33 and sum2= A13+ A22+A31 , then absolute of their difference , i.e. diff= |  sum1-sum2  | .

Input : Please note that here we have taken example of 3*3 , but in this  problem we have to take the dimension of matrix as input . For a matrix to be of dimension N*N . we have to take N as input . And then take N integers as the elements of matrix.  

Output : 

Output should be the absolute of difference. So, we have used abs() function.

Note : It is a humble request to you to please attempt by yourself to the fullest before looking for the code.

Code :

c++ code for above problem 

#include<iostream>
#include<math.h> // For abs() function
using namespace std ;
int main ()
{
    int n,i,j;
    cin>> n ; // n is dimension of the matrix
    int  arr[n][n] , dig1=0,dig2=0,diff ;
for ( i = 0i <ni++)
    {
        for ( j = 0j<nj++)
        {
            cin>> arr[i][j] ;
        }
        
    }
    for ( i = 0i <ni++)// Finding sum of first diagonal elements
    {
        dig1=dig1+arr[i][i];
    }
    for ( i = 0i <ni++)// Finding sum of second diagonal elements
    {
        dig2=dig2arr[i][n-i-1];
    }
    diff=dig1-dig2;
    cout<< abs(diff) ;
}

Thanks.
Plz do visit us for any other doubt if you have .
Happy coding.

Post a Comment

0 Comments