Given a 2-D matrix. The task is to print its corner elements and the sum of the corner elements.
Input: 2 7 5 5 5 8 2 2 4 4 8 8 3 5 6 9 3 4 5 4 3 7 1 3 7 4 2 8 6 9 6 5 6 8 9 8 6 9 9 8 3 6 Output: Corner elements: 2 8 2 6, Corner_Sum = 18 Input: 6 4 6 9 2 6 1 8 5 5 2 2 4 4 1 3 Output: Corner elements: 6 4 9 3, Corner_Sum = 22
Approach: The corner element’s index in a 2-D matrix are:
- left top corner: arr
- right top corner: arr[m-1]
- left bottom corner: arr[n-1]
- right bottom corner: arr[n-1][m-1]
where ‘m’ is the number of columns and ‘n’ is the number of rows.
Below is the implementation of the above approach.
left top corner: 1 right top corner: 4 left bottom corner: 8 right bottom corner: 1 Corner elements Sum = 14
- Check if matrix A can be converted to B by changing parity of corner elements of any submatrix
- Program to print elements of a Matrix row-wise skipping alternate elements
- Remove any corner X rows and columns from a matrix
- Print all the sub diagonal elements of the given square matrix
- Print all the super diagonal elements of the given square matrix
- Print all elements in sorted order from row and column wise sorted matrix
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Move matrix elements in given direction and add elements with same value
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Print matrix in zag-zag fashion
- Print the matrix diagonally downwards
- Print matrix in zig-zag fashion from the last column
- Print an N x M matrix such that each row and column has all the vowels in it
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.