HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Diagonal difference using Linq

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
differencelinqusingdiagonal

Problem

Recently, I have been trying my hands on LINQ. So I've implemented the diagonal difference using Linq in Hackerrank. I know a similar question has been asked in python Diagonal Difference

Here is an excerpt of the problem Hackerrank -Diagonal Difference

Given a square matrix of size N X N , calculate the absolute difference between the sums of its diagonals.

Input Format

The first line contains a single integer, N . The next lines denote the matrix's rows, with each line containing space-separated integers describing the columns.

Output Format

Print the absolute difference between the two sums of the matrix's diagonals as a single integer.

Sample Input

3
11 2 4
4 5 6
10 8 -12


Sample Output

15

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

My Code

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
    static void Main(String[] args) {
        int n = Convert.ToInt32(Console.ReadLine());
        int[][] a = new int[n][];
        for(int a_i = 0; a_i  leftDiagonal=  a.Select((x) => x.ElementAt(value +1));
        int total1 = 0, total2=0;
        foreach (var b in leftDiagonal)
        {
            total1 += b;
            value++;
        }
    
        int value2 = a.Length;
        IEnumerable ans2 = a.Select((x) => x.ElementAt(value2 - 1));
        foreach (var b1 in ans2)
        {
            total2 += b1;
            value2--;
        }
        Console.WriteLine(Math.Abs(total1 - total2));
    }
}


Final Statement

  • I'm looking for improvements in terms of syntax, style, alternatives, and performance . I'm aware of the naming convention but some of these were the pre-defined structure in Hackerrank



  • Using Linq this way is it an overkill for the small data?

Solution

This doesn't look very linq-ish and clean yet. You mixed the algorithm with console output. Everything inside a single method. That needs separation.

Start by creating two extension methods giving you the numbers for the calculations:

public static IEnumerable PrimaryDiagonal(this IEnumerable values)
{
    return values.Select((x, i) => x[i]);
}

public static IEnumerable SecondaryDiagonal(this IEnumerable values)
{
    return values.Reverse().Select((x, i) => x[i]);
}


Now you can calculate whatever you want:

var nums = new[]
{
    new [] { 11, 2, 4 },
    new [] { 4, 5, 6 },
    new [] { 10, 8, - 12 }
};

var primarySum = nums.PrimaryDiagonal().Sum();
var secondarySum = nums.SecondaryDiagonal().Sum();

Code Snippets

public static IEnumerable<T> PrimaryDiagonal<T>(this IEnumerable<T[]> values)
{
    return values.Select((x, i) => x[i]);
}

public static IEnumerable<T> SecondaryDiagonal<T>(this IEnumerable<T[]> values)
{
    return values.Reverse().Select((x, i) => x[i]);
}
var nums = new[]
{
    new [] { 11, 2, 4 },
    new [] { 4, 5, 6 },
    new [] { 10, 8, - 12 }
};

var primarySum = nums.PrimaryDiagonal().Sum();
var secondarySum = nums.SecondaryDiagonal().Sum();

Context

StackExchange Code Review Q#145694, answer score: 4

Revisions (0)

No revisions yet.