• + 0 comments

    Java 15 with streams API:

        public static int flippingMatrix(List<List<Integer>> matrix) {
            int n2 = matrix.size();
            assert n2 % 2 == 0;
            int n = n2 / 2;
            assert 1 <= n && n <= 128;
    
            // similar to a rubiks cube, through a combination of col & row reversals,
            // it is possible to swap any value with another symmetrical across the 
            // middle vertical and horizontal axes. Because of this, we can simply choose
            // the maximum value for the 4 possible positions mirrored across the 4 quadrants.
            int maxSum = IntStream.range(0, n).map(i ->
                IntStream.range(0, n).map(j ->
                    Stream.of(
                        matrix.get(i).get(j), // top left
                        matrix.get(i).get(n2 - j - 1), // top right
                        matrix.get(n2 - i - 1).get(j), // bottom left
                        matrix.get(n2 - i - 1).get(n2 - j - 1) // bottom right
                    )
                        .max(Comparator.naturalOrder())
                        .get()
                ).sum()
            ).sum();
            return maxSum;
        }