We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Yes, it is a good thinking .. If anyone has read Herbert Schildt or Deitel's Java books would have recognized it..
my solution is the same.. some one else should be able to read it quickly..

static int diagonalDifference(int[][] a) {
int primeDi = 0, secondDi = 0;
// could be improved to
// int sum = 0;
for (int i = 0 , j = a[0].length-1; i < a[0].length ; i++, j--) {
primeDi += a[i][i] ;
secondDi += a[i][j] ;
// could be improved to
// sum = a[i][i] - a[i][j] ;
}
return Math.abs(primeDi - secondDi);
// could be improved to
// return Math.abs(sum);
}

The positions of the elements in a matrix can be seen in terms of i and j and the values of i and j comes out to be as shown in the example below of a 3 x 3 matrix:

0,00,10,21,01,11,22,02,12,2

Here, the 1st digit represents the value of i and the 2nd digit represents value of j. There're two diagonals forming in the matrix given above. If we have to identify the diagonal elements in the matrix given above.

staticintdiagonalDifference(int[][]arr){intx=0;inty=arr[0].Length-1;intsum=0;for(intindex=0;index<arr.Length;index++){//sum -= arr[x][x] - arr[x++][y--]; can also be simplified as below:sum=sum-(arr[x][x]-arr[x++][y--]);}returnMath.Abs(sum);}

Now, with the following test input:

1124456108-12

If we've to look at the approach via position wise:
we've done it as:

We'll try to figure out how loop works on the test-input given above:
We've initialized the value of x to be 0
and the value of y to be arr.Length -1.
Note: arr.Length gives us the number of arrays in the Jagged Array, which in our case is 3.
The Jagged array in question is as follows:

After this operation the value of x++ will give 3 and y-- will give -1.
The value of x and y for next loop operation becomes 3 and -1 respectively and this will fail the loop condition and the loop iteration will stop giving 15 as final result.

iterate forward from the first index in a row and backward from the last index in each row during the same loop iteration. Then add 1 to the forward index position, and subtract 1 from the backward index position on each subsequent iteration until you run out of rows!

## Diagonal Difference

You are viewing a single comment's thread. Return to all comments →

haha lol

kya karti rhte ha vellii

hat bosdiko

u dont need two loops for this, in one itself it can be done

how???

I still have one extra variable, which is unnecessary

What happen if input values are changed. I mean couple of more negative values in matrix

Does not affect the outcome, since it is the difference we are after, it is always a positive value

can be done in ((n-1)/2)+1 iteration

For a more readable format:

Cannot read this. always go for a solutions others can read

could you explain?

Very good love you

just in case you didnt know the for loop couldve been written like this for a cleaner look

or as

Great

Yes, it is a good thinking .. If anyone has read Herbert Schildt or Deitel's Java books would have recognized it.. my solution is the same.. some one else should be able to read it quickly..

This comment has helped me understand not only the solution, but

howto get to the solution. From a rookie programmer, thank you for the insight!this is awesome thanks

This is more efficient:

n--; for(int i = 0; i <= n; i++){ sum1 += a[i][i]; sum2 += a[n-i][i]; }

Like this is more efficient:

I think this is the most elegant a clear solution. Well done

This is a fantastic solution.

Beautiful code

Complexity O(N). One Loop.

You will have to change to solution += a[row][row] - a[row++][column--];

Did you try it?

doesn't matter , its abs diff

Hi there,

This solution seems to work beautifully, however I'm a little lost as to how. Any chance you could provide a little runthrough?

Many thanks, M

The positions of the elements in a matrix can be seen in terms of i and j and the values of i and j comes out to be as shown in the example below of a 3 x 3 matrix:

Here, the 1st digit represents the value of i and the 2nd digit represents value of j. There're two diagonals forming in the matrix given above. If we have to identify the diagonal elements in the matrix given above.

Since the square matrix in the question/problem is represented by a Jagged Array (array of arrays). If you don't have an idea of what Jagged Array is, then you might need to first know about the Jagged Array at the following link: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays

Now we analyse the solution:

The solution is as follows:

Now, with the following test input:

If we've to look at the approach via position wise: we've done it as:

We'll try to figure out how loop works on the test-input given above: We've initialized the value of x to be 0 and the value of y to be arr.Length -1. Note: arr.Length gives us the number of arrays in the Jagged Array, which in our case is 3. The Jagged array in question is as follows:

So the size of Jagged Array: arr in question is 3 (which is arr.Length) where each array further contains a simple array of int as can be seen above.

Now let's analyse how loop works:

## 1st round of loop will run as follows:

After this operation the value of x++ will give 1 and y-- will give 1. The value of x and y for next loop operation becomes 1 and 1 respectively.

## 2nd round of loop will run as follows:

After this operation the value of x++ will give 2 and y-- will give 0. The value of x and y for next loop operation becomes 2 and 0 respectively.

## 3rd round of loop will run as follows:

After this operation the value of x++ will give 3 and y-- will give -1. The value of x and y for next loop operation becomes 3 and -1 respectively and this will fail the loop condition and the loop iteration will stop giving 15 as final result.

Thank you very much for your explanation!

My solution was similar but not as elegant. Nice work.

iterate forward from the first index in a row and backward from the last index in each row during the same loop iteration. Then add 1 to the forward index position, and subtract 1 from the backward index position on each subsequent iteration until you run out of rows!

def diagonalDifference(arr): diag1 = 0 diag2 = 0 i1 = 0 i2 = len(arr)-1 for i in range(len(arr)): diag1 += arr[i1][i1] diag2 += arr[i1][i2] i1 += 1 i2 -=1 return abs(diag1 - diag2)

lol why did u got dislikes