Sunday, 16 August 2015

Diagonal Difference Hacker Rank Problem Solution Using C++.


/*
Problem Statement
You are given a square matrix of size N×N. Calculate the absolute difference of the sums across the two main diagonals.
Input Format
The first line contains a single integer N. The next N lines contain N integers (each) describing the matrix.
Constraints
1N100
100A[i]100
Output Format
Output a single integer equal to the absolute difference in the sums across the diagonals.
Sample Input
3
11 2 4
4  5 6
10 8 -12
Sample Output
15
Explanation
The first diagonal of the matrix is:
11
    5
        -12
Sum across the first diagonal = 11+5-12= 4
The second diagonal of the matrix is:
        4
    5
10
Sum across the second diagonal = 4+5+10 = 19
Difference: |4-19| =15
TAG: C++,Array Implementation , Simple Array Sum, Hacker Rank, Array Diagonal Element.
*/

Solution:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main() {
    int n,ar[102][102],d1 = 0,d2 = 0;
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> ar[i][j];
            if(i==j)d1+=ar[i][j];
            if(i+j==n-1)d2+=ar[i][j];
        }
    }
    cout << abs(d1-d2) << endl;
    return 0;
}

5 comments:

  1. Can someone please tell whats wrong with the following code ?




    #include
    #include
    #include
    #include
    #include
    using namespace std;


    int main(){
    int n;
    int sum2 = 0, sum=0;
    cin >> n;
    vector< vector > a(n,vector(n));
    for(int a_i = 0;a_i < n;a_i++){
    for(int a_j = 0;a_j < n;a_j++){
    cin >> a[a_i][a_j];
    }
    }


    for (int i=1;i<=n;i++)
    {
    for (int j=1;j<=n;j++)
    {
    if(i==j)
    { sum = sum + a[i][j];
    break;
    }
    }
    }

    for (int i1=1;i1<=n;i1++)
    {
    for (int j1=n;j1<0;j1--)
    {
    sum2 = sum2 + a[i1][j1];
    j1--;
    break;

    }
    }


    cout << sum2;

    return 0;
    }

    ReplyDelete
  2. i think for sum2 u should not write second for loop instead intialize some variable c to "N"and decrement it after sum2 stmt
    for(int i=0;i<n;i++){
    sum2=sum2+a[i][c];
    c--;
    but it doesn't works for me

    ReplyDelete
  3. #!/bin/python

    import sys


    n = int(input().strip())
    a = []

    for a_i in range(n):
    a.extend(list(map(int,input().strip().split(' '))))

    first_idx,first_sum = 0,0
    for i in range(n):
    first_sum = first_sum + a[first_idx]
    first_idx = first_idx + n + 1

    second_idx,second_sum = n-1,0
    for i in range(n):
    second_sum = second_sum + a[second_idx]
    second_idx = second_idx + n - 1

    print(abs(first_sum - second_sum))

    ReplyDelete
  4. for (int i1=1;i1<=n;i1++)//think i=1,j=3
    {
    for (int j1=n;j1<0;j1--)
    {
    sum2 = sum2 + a[i1][j1];//now a[1][3] will be summed which is crct..
    j1--; /*now i=1,j=1 bcz 2 times u decremented for loop and this statement ....now a[1][1] will be summed whch is not correct...corrrect it.*/
    break;

    ReplyDelete
  5. for (int i1=1;i1<=n;i1++)//think i=1,j=3
    {
    for (int j1=n;j1<0;j1--)
    {
    sum2 = sum2 + a[i1][j1];//now a[1][3] will be summed which is crct..
    j1--; /*now i=1,j=1 bcz 2 times u decremented for loop and this statement ....now a[1][1] will be summed whch is not correct...corrrect it.*/
    break;

    ReplyDelete