Element in a matrix starting from which anti-clockwise traversal ends at the last element
Given a mat[][] of size n X n, the task is to find an element X such that if the anti-clockwise traversal is begun from X then the final element to be printed is mat[n – 1][n – 1].
The anti-clockwise traversal of the matrix, mat[][] =
{{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
starting at element 5 will be 5, 6, 3, 2, 1, 4, 7, 8, 9.
Examples:
Input: mat[][] = {{1, 2}, {3, 4}}
Output: 2
If we start traversing from mat[0][1] i.e. 2 then
we will end up with the element at mat[1][1] which is 4.Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Output: 5
Approach: Starting from the element at mat[n – 1][n – 1], start traversing the matrix in the opposite order i.e. clockwise. When all the elements of the matrix are traversed, the last visited element will be the result.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function to print last element // Of given matrix int printLastElement(int mat[][2], int n) { // Starting row index int si = 0; // Starting column index int sj = 0; // Ending row index int ei = n - 1; // Ending column index int ej = n - 1; // Track the move int direction = 0; // While starting index is less than ending // row index or starting column index // is less the ending column index while (si < ei || sj < ej) { // Switch cases for all direction // Move under all cases for all // directions switch (direction % 4) { case 0: sj++; break; case 1: ei--; break; case 2: ej--; break; case 3: si++; break; } // Increament direction by one // for each case direction++; } // Finally return the last element // If not found return 0 return mat[si][sj]; return 0; } // Driver code int main() { int n = 2; int mat[][2] = { { 1, 2 }, { 3, 4 } }; cout << printLastElement(mat, n); return 0; } |
Java
// Java implementation of the approach class GfG { // Function to print last element // Of given matrix static int printLastElement(int mat[][], int n) { // Starting row index int si = 0; // Starting column index int sj = 0; // Ending row index int ei = n - 1; // Ending column index int ej = n - 1; // Track the move int direction = 0; // While starting index is less than ending // row index or starting column index // is less the ending column index while (si < ei || sj < ej) { // Switch cases for all direction // Move under all cases for all // directions switch (direction % 4) { case 0: sj++; break; case 1: ei--; break; case 2: ej--; break; case 3: si++; break; } // Increament direction by one // for each case direction++; } // Finally return the last element // If not found return 0 return mat[si][sj]; } // Driver code public static void main(String[] args) { int n = 2; int mat[][] = new int[][]{{ 1, 2 }, { 3, 4 }}; System.out.println(printLastElement(mat, n)); } } // This code is contributed by Prerna Saini |
Python3
# Python 3 implementation of the approach # Function to print last element # Of given matrix def printLastElement(mat, n): # Starting row index si = 0 # Starting column index sj = 0 # Ending row index ei = n - 1 # Ending column index ej = n - 1 # Track the move direction = 0 # While starting index is less than ending # row index or starting column index # is less the ending column index while (si < ei or sj < ej): # Switch cases for all direction # Move under all cases for all # directions if (direction % 4 == 0): sj += 1 if (direction % 4 == 1): ei -= 1 if (direction % 4 == 2): ej -= 1 if (direction % 4 == 3): si += 1 # Increament direction by one # for each case direction += 1 # Finally return the last element # If not found return 0 return mat[si][sj] return 0 # Driver code if __name__ == '__main__': n = 2 mat = [[1, 2], [3, 4 ]] print(printLastElement(mat, n)) # This code is contributed by # Surendra_Gangwar |
C#
// C# implementation of the above approach using System; class GFG { // Function to print last element // Of given matrix static int printLastElement(int [,]mat, int n) { // Starting row index int si = 0; // Starting column index int sj = 0; // Ending row index int ei = n - 1; // Ending column index int ej = n - 1; // Track the move int direction = 0; // While starting index is less than ending // row index or starting column index // is less the ending column index while (si < ei || sj < ej) { // Switch cases for all direction // Move under all cases for all // directions switch (direction % 4) { case 0: sj++; break; case 1: ei--; break; case 2: ej--; break; case 3: si++; break; } // Increament direction by one // for each case direction++; } // Finally return the last element // If not found return 0 return mat[si, sj]; } // Driver code public static void Main() { int n = 2; int [,]mat = {{ 1, 2 }, { 3, 4 }}; Console.WriteLine(printLastElement(mat, n)); } } // This code is contributed by Ryuga |
PHP
2
Recommended Posts:
- Rotate each ring of matrix anticlockwise by K elements
- Sum of columns of a 2-D Matrix where first element is odd
- Minimum element of each row and each column in a matrix
- Find maximum element of each row in a matrix
- Search element in a sorted matrix
- Problem of 8 Neighbours of an element in a 2-D Matrix
- Count majority element in a matrix
- Find maximum element of each column in a matrix
- Find the element in the matrix generated by given rules
- Search element in a Spirally sorted Matrix
- Count rows in a matrix that consist of same element
- Program to find the maximum element in a Matrix
- Print K'th element in spiral form of matrix
- Return previous element in an expanding matrix
- Replace every matrix element with maximum of GCD of row or column
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



