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++
#include <iostream>
using namespace std;
int printLastElement( int mat[][2], int n)
{
int si = 0;
int sj = 0;
int ei = n - 1;
int ej = n - 1;
int direction = 0;
while (si < ei || sj < ej) {
switch (direction % 4) {
case 0:
sj++;
break ;
case 1:
ei--;
break ;
case 2:
ej--;
break ;
case 3:
si++;
break ;
}
direction++;
}
return mat[si][sj];
return 0;
}
int main()
{
int n = 2;
int mat[][2] = { { 1, 2 }, { 3, 4 } };
cout << printLastElement(mat, n);
return 0;
}
|
Java
import java.io.*;
class GfG
{
static int printLastElement( int mat[][], int n)
{
int si = 0 ;
int sj = 0 ;
int ei = n - 1 ;
int ej = n - 1 ;
int direction = 0 ;
while (si < ei || sj < ej)
{
switch (direction % 4 )
{
case 0 :
sj++;
break ;
case 1 :
ei--;
break ;
case 2 :
ej--;
break ;
case 3 :
si++;
break ;
}
direction++;
}
return mat[si][sj];
}
public static void main(String[] args)
{
int n = 2 ;
int mat[][] = new int [][]{{ 1 , 2 }, { 3 , 4 }};
System.out.println(printLastElement(mat, n));
}
}
|
Python3
def printLastElement(mat, n):
si = 0
sj = 0
ei = n - 1
ej = n - 1
direction = 0
while (si < ei or sj < ej):
if (direction % 4 = = 0 ):
sj + = 1
if (direction % 4 = = 1 ):
ei - = 1
if (direction % 4 = = 2 ):
ej - = 1
if (direction % 4 = = 3 ):
si + = 1
direction + = 1
return mat[si][sj]
return 0
if __name__ = = '__main__' :
n = 2
mat = [[ 1 , 2 ], [ 3 , 4 ]]
print (printLastElement(mat, n))
|
C#
using System;
class GFG
{
static int printLastElement( int [,]mat, int n)
{
int si = 0;
int sj = 0;
int ei = n - 1;
int ej = n - 1;
int direction = 0;
while (si < ei || sj < ej)
{
switch (direction % 4)
{
case 0:
sj++;
break ;
case 1:
ei--;
break ;
case 2:
ej--;
break ;
case 3:
si++;
break ;
}
direction++;
}
return mat[si, sj];
}
public static void Main()
{
int n = 2;
int [,]mat = {{ 1, 2 }, { 3, 4 }};
Console.WriteLine(printLastElement(mat, n));
}
}
|
PHP
<?php
function printLastElement( $mat , $n )
{
$si = 0;
$sj = 0;
$ei = $n - 1;
$ej = $n - 1;
$direction = 0;
while ( $si < $ei || $sj < $ej )
{
switch ( $direction % 4)
{
case 0:
$sj ++;
break ;
case 1:
$ei --;
break ;
case 2:
$ej --;
break ;
case 3:
$si ++;
break ;
}
$direction ++;
}
return $mat [ $si ][ $sj ];
return 0;
}
$n = 2;
$mat = array ( array (1, 2),
array (3, 4));
echo printLastElement( $mat , $n );
?>
|
Javascript
<script>
function printLastElement(mat , n) {
var si = 0;
var sj = 0;
var ei = n - 1;
var ej = n - 1;
var direction = 0;
while (si < ei || sj < ej) {
switch (direction % 4) {
case 0:
sj++;
break ;
case 1:
ei--;
break ;
case 2:
ej--;
break ;
case 3:
si++;
break ;
}
direction++;
}
return mat[si][sj];
}
var n = 2;
var mat = [[ 1, 2 ], [ 3, 4 ] ];
document.write(printLastElement(mat, n));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)