Hackerrank Diagonal Difference problem solution in C | Algorithms - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Saturday, May 6, 2017

Hackerrank Diagonal Difference problem solution in C | Algorithms

Hackerrank Diagonal Difference problem solution in C | Algorithms


HackerRank Online Judge Solution  | Data structures
HackerRank Main Problem Link - https://www.hackerrank.com/challenges/diagonal-difference



Problem Name: HackerRank Problem  Diagonal Difference
Problem Number : HackerRank Problem Diagonal Difference Solution
Online Judge : HackerRank Online Judge Solution
Category: Algorithm
Solution Language : C, C plus plus

Hackerrank Diagonal Difference problem solution in C | Algorithms


HackerRank Solution Diagonal Difference Code in C:


#include<stdio.h>
int main()
{
    int n, r, c, i;
    int number;
    int primary_diagonal = 0, secondary_diagonal = 0;

    scanf("%d", &n);
    int arr[n][n];

    for(r = 0; r < n; r++)
    {
        for(c= 0; c < n; c++)
        {
            scanf("%d", &arr[r][c]);
        }
    }

    //Store
    int temp_n = n-1;
    for(r = 0; r < n; r++)
    {
        primary_diagonal += arr[r][r];
        secondary_diagonal += arr[r][temp_n];
        temp_n--;
    }

    //To comparison and subtract from one another
    int dif = (primary_diagonal > secondary_diagonal)? primary_diagonal - secondary_diagonal : secondary_diagonal - primary_diagonal;
    printf("%d\n", dif);

    return 0;
}

No comments:

Post a Comment