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.
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.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Diagonal Difference
You are viewing a single comment's thread. Return to all comments →
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.