• + 1 comment

    The 4 matrix cells listed below are to be checked for the max value possible at arr[i][j] location. We can add the max value obtained at every step and then return their sum. Try out the below written 4 matrix cells to figure out the pattern:-

    arr[i][j], arr[i][2*n-j-1], arr[2*n-i-1][j], arr[2*n-i-1][2*n-j-1]

        int sum=0;
        for(int i=0;i<n;i++){
                for(int j=0;j<n;j++){
                    sum+=(int)Math.max(Math.max(arr[i][j],arr[i][2*n-j-1]),Math.max(arr[2*n-i-1][j],arr[2*n-i-1][2*n-j-1]));
                }
        }
        return sum;