Diagonal Difference

  • + 0 comments

    Java 8 solution

    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);
    
    }