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.
public static int diagonalDifference(List<List<Integer>> arr) {
// Write your code here
int sumElementLeftToRightDiagonal= IntStream
.range(0,arr.size())
.mapToObj(i->arr.get(i).get(i))
.collect(toList())
.stream()
.reduce(0,Integer::sum);
int sumElementRightToLeftDiagonal=IntStream
.range(0, arr.size())
.mapToObj(i->arr.get(i).get(arr.size()-1-i))
.collect(toList())
.stream()
.reduce(0,Integer::sum);
return Math.abs(sumElementLeftToRightDiagonal-sumElementRightToLeftDiagonal);
}
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 →
Java 8 solution