HackerRank ‘Diagonal Difference’ Solution | MartinKysel.com
You are given a square matrix of size N×N. Calculate the absolute difference of the sums across the two main diagonals.
Given two strings (they can be of same or different length) help her in finding out the minimum number of character deletions required to make two strings anagrams. Any characters can be deleted from any of the strings.
http://www.java-fries.com/2016/02/diagonal-difference/
Handle input and compute at same time.
https://github.com/derekhh/HackerRank/blob/master/diagonal-difference.cpp
No need to use 2 variable to compute sum separately.
Read full article from HackerRank ‘Diagonal Difference’ Solution | MartinKysel.com
You are given a square matrix of size N×N. Calculate the absolute difference of the sums across the two main diagonals.
Given two strings (they can be of same or different length) help her in finding out the minimum number of character deletions required to make two strings anagrams. Any characters can be deleted from any of the strings.
http://www.java-fries.com/2016/02/diagonal-difference/
private static int calculateDiagonalDifference(int[][] arr) {
int len = arr.length;
int leftDiagonalSum = 0;
int rightDiagonalSum = 0;
int index = 0;
while(index < len) {
leftDiagonalSum += arr[index][index];
rightDiagonalSum += arr[index][len-1-index];
index++;
}
return Math.abs(leftDiagonalSum - rightDiagonalSum);
}
https://bubualgorithm.wordpress.com/2015/06/23/hackerrank-algorithm-warmup-diagonal-difference/Handle input and compute at same time.
int
main()
{
int
n = 0, difference = 0, tmp = 0;
cin >> n;
for
(
int
i = 0; i < n; ++i) {
for
(
int
j = 0; j < n; ++j) {
cin >> tmp;
if
(j == i) {
difference += tmp;
}
if
(j == n - 1 - i) {
difference -= tmp;
}
}
}
difference =
abs
(difference);
std::cout << difference << std::endl;
return
0;
}
def
getDiagonalDifference(v):
diff
=
0
v_len
=
len
(v)
for
idx
in
xrange
(v_len):
diff
+
=
v[idx][idx]
diff
-
=
v[idx][v_len
-
idx
-
1
]
return
abs
(diff)
https://github.com/derekhh/HackerRank/blob/master/diagonal-difference.cpp
No need to use 2 variable to compute sum separately.
Read full article from HackerRank ‘Diagonal Difference’ Solution | MartinKysel.com