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 ; } // Increment 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 import java.io.*; 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 ; } // Increment 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 # Increment 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 ; } // Increment 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
<?php // PHP implementation of the approach // Function to print last element // Of given matrix function 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 || $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 ; } // Increment 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 $n = 2; $mat = array ( array (1, 2), array (3, 4)); echo printLastElement( $mat , $n ); // This code is contributed by Akanksha Rai ?> |
Javascript
<script> // JavaScript implementation of the approach // Function to print last element // Of given matrix function printLastElement(mat , n) { // Starting row index var si = 0; // Starting column index var sj = 0; // Ending row index var ei = n - 1; // Ending column index var ej = n - 1; // Track the move var 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 ; } // Increment direction by one // for each case direction++; } // Finally return the last element // If not found return 0 return mat[si][sj]; } // Driver code var n = 2; var mat = [[ 1, 2 ], [ 3, 4 ] ]; document.write(printLastElement(mat, n)); // This code contributed by gauravrajput1 </script> |
2
Time Complexity: O(N2)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!