diagonal difference hackerrank solution in python
def diagonalDifference(arr):
# Write your code here
d1 =0
d2=0
for i in range(len(arr[0])):
d1 += arr[i][i]
d2 += arr[i][(len(arr[0])-i-1)]
return abs(d1-d2)
diagonal difference hackerrank solution in python
def diagonalDifference(arr):
# Write your code here
d1 =0
d2=0
for i in range(len(arr[0])):
d1 += arr[i][i]
d2 += arr[i][(len(arr[0])-i-1)]
return abs(d1-d2)
diagonal difference hackerrank solution in java 8 using list
public static int diagonalDifference(List<List<Integer>> arr) {
// Write your code here
int leftd=0;
int rightd=0;
int n=arr.size();
for(int i=0;i<n;i++)
{
leftd+=arr.get(i).get(i);
rightd+=arr.get(i).get(n-i-1);
}
int absd=Math.abs(leftd-rightd);
return absd;
}
diagonal difference hackerrank solution in python
def diagonalDifference(arr):
# Write your code here
d1 = sum([arr[x][x] for x in range(len(arr))])
d2 = sum([arr[x][n - 1 - x] for x in range(len(arr))])
return(abs(d1 - d2))
hackerrank diagonal difference
var right int32
var left int32
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr); j++ {
if i == j {
right += int32(arr[i][j])
}
if i+j == len(arr)-1 {
left += int32(arr[i][j])
}
}
}
sum := right - left
if sum < 0 {
sum -= sum
}
return sum
}
# step one
0 == 0 = 11 // true
1 == 0 = 2
2 == 0 = 4
0 == 1 = 4
1 == 1 = 5 // true
2 == 1 = 6
0 == 2 = 10
1 == 2 = 8
2 == 2 = -12 // true
= 11 + 5 - 12 = 4
# step two
0 == 2 = 11
1 == 2 = 2
2 == 2 = 4 // true
1 == 2 = 4
2 == 2 = 5 // true
3 == 2 = 6
2 == 2 = 10 // t rue
3 == 2 = 8
4 == 2 = 12
= 4 + 5 + 10 = 19
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us