How to sum the corners of a matrix in Java

1 Answer

0 votes
import java.util.Arrays;
  
public class MatrixCornerSum {
    public static int sumMatrixCorners(int[][] matrix) {
        return matrix[0][0] + 
               matrix[0][matrix[0].length - 1] + 
               matrix[matrix.length - 1][0] + 
               matrix[matrix.length - 1][matrix[0].length - 1];
    }
  
    public static void main(String[] args) {
        int[][] matrix = {
            { 1, 2, 3, 4 },
            { 5, 6, 7, 8 },
            { 9, 3, 2, 0 }
        };
  
        System.out.println(sumMatrixCorners(matrix));
    }
}
  
  
    
/*
run:
    
14
    
*/

 



answered May 19, 2024 by avibootz
edited May 20, 2024 by avibootz

Related questions

1 answer 83 views
1 answer 155 views
1 answer 153 views
1 answer 126 views
126 views asked May 20, 2024 by avibootz
1 answer 95 views
95 views asked May 20, 2024 by avibootz
1 answer 97 views
1 answer 109 views
...