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;
}

8 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
  6. #include
    #include
    #include
    #include
    #include
    #include
    #include
    /*
    go through my logic:
    dhananjay ahire: deep learning researcher at pune university, india
    feel free contact me if any dout then: 9552039479

    */
    int main()
    {
    int n;
    scanf("%d",&n);
    int a[n][n];
    for(int i = 0; i < n; i++)
    {
    for(int j = 0; j < n; j++)
    {
    scanf("%d",&a[i][j]);
    }
    }
    /*]int i=0;
    int j=0;*/
    int diff2=0;
    int diff1=0;
    for(int i=0;i=0;j--)
    {
    if(i+j==n-1)
    {
    diff2 = diff2+ a[i][j];
    break;
    }
    }
    }
    printf("%d\n",abs(diff1-diff2));
    /*another method which will gives a same output
    result1=a[i][j]+a[i+1][j+1]+a[i+2][j+2];
    result2=a[i][j+2]+a[i+1][j+1]+a[i+2][j];
    result=abs(result1-result2);
    printf("%d",result);*/
    return 0;
    }

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Guys whats wrong with loop of sum2 ...


    #include
    #include

    int main()
    {
    int n,sum1=0,sum2=0,newsum=0,a_i,a_j;
    scanf("%d",&n);
    int a[n][n];
    for( a_i = 0; a_i < n; a_i++)
    {
    for ( a_j = 0; a_j < n; a_j++)
    {

    scanf("%d",&a[a_i][a_j]);
    }
    }
    for( a_i = 0; a_i < n; a_i++)
    {
    sum1=sum1+a[a_i][a_i];
    }
    printf("%d \n",sum1);

    for( a_i = 0; a_i=0;a_j++)
    {
    sum2= sum2+a[a_i][a_j];
    }
    }
    newsum=abs(sum1-sum2);
    printf("%d",newsum);

    return 0;
    }

    ReplyDelete