0
public static int diagonalDifference(List<List<int>> arr)
{
    int sumRightToLeft = 0;
    int sumLeftToRight = 0;

    for(int i = 0; i < arr.Count; i++)
    {
        if(arr[i].Count == arr[i+1].Count)
        {
            sumRightToLeft += arr[i][i];
            sumLeftToRight += arr[i][arr[i].Count-i-1];
        }
        else
            continue;
    }

    int abs = Math.Abs(sumRightToLeft - sumRightToLeft);
    return abs;
}

When I tried difference to diagonal on matrix I'll take Index out of Range Exception unlike below.

Unhandled Exception: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index

I could not understand that didn't happen when I check If block cause which boundaries true. What is my mistake If u find please help me I have not figured out that

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Please review standard duplicate. Usually something like `arr[i+1]` is a problem - make sure your code does not do such things. If duplicate still does not provide an answer review [MCVE] guidance on posting code and make sure to remove all unrelated code and provide input inline in the sample. – Alexei Levenkov May 27 '20 at 00:29
  • Could be I duplicate this post cause what I want to do clearly understand this function sorry about that but Function parameter takes a jagged list but matrix has to be square so I have to compare list first-dimensional item `arr[i]` and `arr[i+1]` however exception reason couldn't be that. Issue the cause `arr.Count` in `for` block hardly which is true notation what should I do? – aarnautovic May 27 '20 at 00:50

0 Answers0