In this post, we will be covering Diagonal Difference from hackerrank, in which we have to calculate the difference between the summation value of Diagonals. The solution is carried out in C++ with the required sets of variables, initializations, looping to get the required output.
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
For example, the square matrix arr is shown below:
Complete the diagnoldifference function in the editor below.
diagonalDifference takes the following parameter:
- int arr[n][m]: an array of integers.
- int: the absolute diagonal difference
The first line contains a single integer, n, the number of rows and columns in the square matrix arr.
Each of the next n lines describes a row,arr[i] , and consists of n space-separated integers arr[i][j].
Return the absolute difference between the sums of the matrix's two diagonals as a single integer.
Let's write the code for Problem
We will be solving this problem by calculating the sum of elements present diagonally in the given square matrix and then will be finding the absolute difference of two summed values. If you do not have an idea on how to use absolute function - refer to this article on absolute function in C++
Get a clear explanation of this problem on HackerRank
Sample Test Case Passed Successfully
This problem and its description are taken from HackerRank - Solution is provided by The Coding Bro in sole purpose of Learning and Education - Visit HackerRank to solve more problems.
If you liked this post, then share this with your friends and if anything is confusing or incorrect then let me know in the comment section.
Thanks from my side, this is Mayank, keep learning and exploring !!