Sunday, 2 April 2017

HackerRank solution for Diagonal Difference in C++

Problem:- diagonal difference hacker rank solution in Java or diagonal difference hacker rank solution c++ or plus-minus hacker rank solution c++ or diagonal difference program in c or diagonal difference solution in c or diagonal difference hacker rank solution in python or diagonal difference in java or diagonal difference hacker rank solution in c or Hacker Rank Solution For Diagonal Difference in C++ or Diagonal Difference in C++ or Hacker Rank solution for Diagonal Difference in C++.

Check This-
Hacker rank Solution for C++ Sub Domain Introduction, Classes, STL, Inheritance.


Logic:- So logic is very simple in this diagonal difference hacker rank problem we have to find the diagonal sum of matrix  and after find the sum just simply find out the difference between both diagonal sum and if the difference of both diagonal matrices is negative then find the Mod or in the end print the output. See the above logic solution with an example in explanation.

Explanation:- So as we already know what is the logic so without wasting time lets just implement the above logic in our diagonal difference hacker rank problem with an example. Take a MATRIX with the same number of row and column, see the example below.

Example 

3          //Row and Column Size

11     2      4
4       5      6
10     8    -12

so our matrix is 3 * 3 and diagonals of the matrix are given below

First Diagonal

11         
        5
              -12

and the sum of Diagonal is 11 + 5 + (-12) = 4

Second Diagonal

             4
      5 
10

and the sum of second diagonal is 4 + 5 + 10 = 19

Now the last step is to find the Difference of between sum of diagonal so add first diagonal and second diagonal after that mod the difference so | 4 - 19| = 15. Hence we got our solution.

Copy the full solution or colored code and paste into the hacker rank editor and click to Run Code if the code runs successfully then click to Submit Code. Before pasting the code into the editor make sure you have chosen c++ editor in the top right drop down option.

Submit your solution here:- Click here

Tip:- Before copy the solution I recommended please read this full article, this will help you to build your own logic. 

Solution:-

#include <iostream>
using namespace std;

int main() 
{
int N,lsum=0,rsum=0;

cin>>N;
int a[N][N];

for(int i=0;i<N;i++)
{   
 for(int j=0;j<N;j++)
 {
 cin>>a[i][j];
 if(i==j)
 {
 lsum+=a[i][j];
 }
 }
 } 
 for(int i=0;i<N;i++)
 {
 for(int j=N-1-i;j>=0;)
 {
 rsum+=a[i][j];
 break;
 }
 } 
 cout<<abs(lsum-rsum)<<endl;
return 0;
}

Output:-

Hacker Rank solution for Diagonal Difference in C++

You May Also Like

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment