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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find all matrix elements which are minimum in their row and maximum in their column
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Check if matrix A can be converted to B by changing parity of corner elements of any submatrix
- Remove any corner X rows and columns from a matrix
- Minimum cost to reach from the top-left to the bottom-right corner of a matrix
- Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2
- Print the Vowels in the Order of their occurrence in the given Matrix
- Program to print elements of a Matrix row-wise skipping alternate elements
- Minimum cells traversed to reach corner where every cell represents jumps
- Minimum distance to the corner of a grid from source
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Check if the first and last digit of number N is prime and their sum is less than K
- 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
- Print N numbers such that their product is a Perfect Cube
- Construct a matrix with sum equal to the sum of diagonal elements
- Print sum of matrix and its mirror image
- Print all the sub diagonal elements of the given square matrix
- Print all the super diagonal elements of the given square matrix
- Move matrix elements in given direction and add elements with same value