Square Matrix

Description:

Given a square matrix of size NN, calculate the absolute difference between the sums of its diagonals. Input The first line contains a single integer N. The next N lines denote the matrix's rows, with each line containing N space-separated integers describing the columns. Output Print the absolute difference between the two sums of the matrix's diagonals as a single integer. Constraints 1<=N<=10 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 Test Case 1 Input (stdin) 3 11 2 4 4 5 6 10 8 -12 Expected Output 15 Test Case 2 Input (stdin) 3 4 5 5 3 9 6 7 4 6 Expected Output 2

Program :

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

typedef long long int lli;

typedef long int li;

#define MAX(a,b) a>b?a:b

#define MIN(a,b) a>b?b:a

int main()

{

    int n,a,i,j,p,q,x=0,y=0;

    scanf("%d",&n);

    p=1;q=n;

    for(i=1;i<=n;i++)

    {

        for(j=1;j<=n;j++)

        {

            scanf("%d",&a);

            if(i==j)

            {

                x+=a;

            }

            if(i==p && j==q)

            {

                y+=a;

                p++;

                q--;

            }

        }

    }

    printf("%d",x-y>0 ? x-y : y-x);

  return 0;

}