Print K’th element in spiral form of matrix
Last Updated :
12 Dec, 2022
Given a 2D Matrix of order n X m, print K’th element in the spiral form of the matrix. See the following examples.
Examples:
Input: mat[][] =
{{1, 2, 3, 4}
{5, 6, 7, 8}
{9, 10, 11, 12}
{13, 14, 15, 16}}
k = 6
Output: 12
Explanation: The elements in spiral order is
1, 2, 3, 4, 8, 12, 16, 15...
so the 6th element is 12
Input: mat[][] =
{{1, 2, 3, 4, 5, 6}
{7, 8, 9, 10, 11, 12}
{13, 14, 15, 16, 17, 18}}
k = 17
Output: 10
Explanation: The elements in spiral order is
1, 2, 3, 4, 5, 6, 12, 18, 17,
16, 15, 14, 13, 7, 8, 9, 10, 11
so the 17 th element is 10.
Simple Approach: One simple solution is to start traversing matrix in spiral form Print Spiral Matrix and start a counter i.e; count = 0. Whenever count gets equal to K, print that element.
- Algorithm:
- Keep a variable count = 0 to store the count.
- Traverse the matrix in spiral from start to end.
- Increase the count by 1 for every iteration.
- If the count is equal to the given value of k print the element and break.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define R 3
#define C 6
void spiralPrint( int m, int n, int a[R][C], int c)
{
int i, k = 0, l = 0;
int count = 0;
while (k < m && l < n) {
for (i = l; i < n; ++i) {
count++;
if (count == c)
cout << a[k][i] << " " ;
}
k++;
for (i = k; i < m; ++i) {
count++;
if (count == c)
cout << a[i][n - 1] << " " ;
}
n--;
if (k < m) {
for (i = n - 1; i >= l; --i) {
count++;
if (count == c)
cout << a[m - 1][i] << " " ;
}
m--;
}
if (l < n) {
for (i = m - 1; i >= k; --i) {
count++;
if (count == c)
cout << a[i][l] << " " ;
}
l++;
}
}
}
int main()
{
int a[R][C] = { { 1, 2, 3, 4, 5, 6 },
{ 7, 8, 9, 10, 11, 12 },
{ 13, 14, 15, 16, 17, 18 } },
k = 17;
spiralPrint(R, C, a, k);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int R = 3 ;
static int C = 6 ;
static void spiralPrint( int m, int n, int [][] a, int c)
{
int i, k = 0 , l = 0 ;
int count = 0 ;
while (k < m && l < n) {
for (i = l; i < n; ++i) {
count++;
if (count == c)
System.out.println(a[k][i]+ " " );
}
k++;
for (i = k; i < m; ++i) {
count++;
if (count == c)
System.out.println(a[i][n - 1 ]+ " " );
}
n--;
if (k < m) {
for (i = n - 1 ; i >= l; --i) {
count++;
if (count == c)
System.out.println(a[m - 1 ][i]+ " " );
}
m--;
}
if (l < n) {
for (i = m - 1 ; i >= k; --i) {
count++;
if (count == c)
System.out.println(a[i][l]+ " " );
}
l++;
}
}
}
public static void main (String[] args)
{
int a[][] = { { 1 , 2 , 3 , 4 , 5 , 6 },
{ 7 , 8 , 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 , 17 , 18 } };
int k = 17 ;
spiralPrint(R, C, a, k);
}
}
|
Python3
R = 3
C = 6
def spiralPrint(m, n, a, c):
k = 0
l = 0
count = 0
while (k < m and l < n):
for i in range (l,n):
count + = 1
if (count = = c):
print (a[k][i] , end = " " )
k + = 1
for i in range (k,m):
count + = 1
if (count = = c):
print (a[i][n - 1 ],end = " " )
n - = 1
if (k < m):
for i in range (n - 1 ,l - 1 , - 1 ):
count + = 1
if (count = = c):
print (a[m - 1 ][i],end = " " )
m - = 1
if (l < n):
for i in range (m - 1 ,k - 1 , - 1 ):
count + = 1
if (count = = c):
print (a[i][l],end = " " )
l + = 1
a = [[ 1 , 2 , 3 , 4 , 5 , 6 ],[ 7 , 8 , 9 , 10 , 11 , 12 ],[ 13 , 14 , 15 , 16 , 17 , 18 ]]
k = 17
spiralPrint(R, C, a, k)
|
C#
using System;
class GFG
{
static int R = 3;
static int C = 6;
static void spiralPrint( int m, int n,
int [,] a, int c)
{
int i, k = 0, l = 0;
int count = 0;
while (k < m && l < n)
{
for (i = l; i < n; ++i)
{
count++;
if (count == c)
Console.WriteLine(a[k, i] + " " );
}
k++;
for (i = k; i < m; ++i)
{
count++;
if (count == c)
Console.WriteLine(a[i, n - 1] + " " );
}
n--;
if (k < m)
{
for (i = n - 1; i >= l; --i)
{
count++;
if (count == c)
Console.WriteLine(a[m - 1, i] + " " );
}
m--;
}
if (l < n)
{
for (i = m - 1; i >= k; --i)
{
count++;
if (count == c)
Console.WriteLine(a[i, l] + " " );
}
l++;
}
}
}
static void Main()
{
int [,] a = { { 1, 2, 3, 4, 5, 6 },
{ 7, 8, 9, 10, 11, 12 },
{ 13, 14, 15, 16, 17, 18 } };
int k = 17;
spiralPrint(R, C, a, k);
}
}
|
Javascript
<script>
let R = 3;
let C = 6;
function spiralPrint(m, n, a, c)
{
let i, k = 0, l = 0;
let count = 0;
while (k < m && l < n) {
for (i = l; i < n; ++i) {
count++;
if (count == c)
document.write(a[k][i] + " " );
}
k++;
for (i = k; i < m; ++i) {
count++;
if (count == c)
document.write(a[i][n - 1] + " " );
}
n--;
if (k < m) {
for (i = n - 1; i >= l; --i) {
count++;
if (count == c)
document.write(a[m - 1][i] + " " );
}
m--;
}
if (l < n) {
for (i = m - 1; i >= k; --i) {
count++;
if (count == c)
document.write(a[i][l] + " " );
}
l++;
}
}
}
let a = [ [ 1, 2, 3, 4, 5, 6 ],
[ 7, 8, 9, 10, 11, 12 ],
[ 13, 14, 15, 16, 17, 18 ] ],
k = 17;
spiralPrint(R, C, a, k);
</script>
|
Complexity Analysis:
- Time Complexity: O(R*C), A single traversal of matrix is needed so the Time Complexity is O(R*C).
- Space Complexity: O(1), constant space is required.
Efficient Approach: While traversing the array in spiral order, a loop is used to traverse the sides. So if it can be found out that the kth element is in the given side then the kth element can be found out in constant time. This can be done recursively as well as iteratively.
- Algorithm :
- Traverse the matrix in form of spiral or cycles.
- So a cycle can be divided into 4 parts, so if the cycle is of size m X n.
- Element is in first row, i.e k <= m
- Element is in last column, i.e k <= (m+n-1)
- Element is in last row, i.e. k <= (m+n-1+m-1)
- Element is in first column, i.e k <= (m+n-1+m-1+n-2)
- If any of the above conditions meet then the kth element can be found is constant time.
- Else remove the cycle from the array and recursively call the function.
Implementation:
C++
#include <bits/stdc++.h>
#define MAX 100
using namespace std;
int findK( int A[MAX][MAX], int n, int m, int k)
{
if (n < 1 || m < 1)
return -1;
if (k <= m)
return A[0][k - 1];
if (k <= (m + n - 1))
return A[(k - m)][m - 1];
if (k <= (m + n - 1 + m - 1))
return A[n - 1][m - 1 - (k - (m + n - 1))];
if (k <= (m + n - 1 + m - 1 + n - 2))
return A[n - 1 - (k - (m + n - 1 + m - 1))][0];
return findK(( int (*)[MAX])(&(A[1][1])), n - 2,
m - 2, k - (2 * n + 2 * m - 4));
}
int main()
{
int a[MAX][MAX] = { { 1, 2, 3, 4, 5, 6 },
{ 7, 8, 9, 10, 11, 12 },
{ 13, 14, 15, 16, 17, 18 } };
int k = 17;
cout << findK(a, 3, 6, k) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int MAX = 100 ;
static int findK( int A[][], int i, int j,
int n, int m, int k)
{
if (n < 1 || m < 1 )
return - 1 ;
if (k <= m)
return A[i + 0 ][j + k - 1 ];
if (k <= (m + n - 1 ))
return A[i + (k - m)][j + m - 1 ];
if (k <= (m + n - 1 + m - 1 ))
return A[i + n - 1 ][j + m - 1 - (k - (m + n - 1 ))];
if (k <= (m + n - 1 + m - 1 + n - 2 ))
return A[i + n - 1 - (k - (m + n - 1 + m - 1 ))][j + 0 ];
return findK(A, i + 1 , j + 1 , n - 2 ,
m - 2 , k - ( 2 * n + 2 * m - 4 ));
}
public static void main(String args[])
{
int a[][] = { { 1 , 2 , 3 , 4 , 5 , 6 },
{ 7 , 8 , 9 , 10 , 11 , 12 },
{ 13 , 14 , 15 , 16 , 17 , 18 } };
int k = 17 ;
System.out.println(findK(a, 0 , 0 , 3 , 6 , k));
}
}
|
Python3
MAX = 100
def findK(A, n, m, k):
if (n < 1 or m < 1 ):
return - 1
if (k < = m):
return A[ 0 ][k - 1 ]
if (k < = (m + n - 1 )):
return A[(k - m)][m - 1 ]
if (k < = (m + n - 1 + m - 1 )):
return A[n - 1 ][m - 1 - (k - (m + n - 1 ))]
if (k < = (m + n - 1 + m - 1 + n - 2 )):
return A[n - 1 - (k - (m + n - 1 + m - 1 ))][ 0 ]
A.pop( 0 )
[j.pop( 0 ) for j in A]
return findK(A, n - 2 , m - 2 , k - ( 2 * n + 2 * m - 4 ))
a = [[ 1 , 2 , 3 , 4 , 5 , 6 ],[ 7 , 8 , 9 , 10 , 11 , 12 ],
[ 13 , 14 , 15 , 16 , 17 , 18 ]]
k = 17
print (findK(a, 3 , 6 , k))
|
C#
using System;
class GFG
{
static int findK( int [,] A, int i, int j,
int n, int m, int k)
{
if (n < 1 || m < 1)
return -1;
if (k <= m)
return A[i + 0, j + k - 1];
if (k <= (m + n - 1))
return A[i + (k - m), j + m - 1];
if (k <= (m + n - 1 + m - 1))
return A[i + n - 1, j + m - 1 - (k - (m + n - 1))];
if (k <= (m + n - 1 + m - 1 + n - 2))
return A[i + n - 1 - (k - (m + n - 1 + m - 1)), j + 0];
return findK(A, i + 1, j + 1, n - 2,
m - 2, k - (2 * n + 2 * m - 4));
}
static void Main()
{
int [,] a = { { 1, 2, 3, 4, 5, 6 },
{ 7, 8, 9, 10, 11, 12 },
{ 13, 14, 15, 16, 17, 18 } };
int k = 17;
Console.WriteLine(findK(a, 0, 0, 3, 6, k));
}
}
|
Javascript
<script>
let MAX = 100;
function findK(A,i,j,n,m,k)
{
if (n < 1 || m < 1)
return -1;
if (k <= m)
return A[i + 0][j + k - 1];
if (k <= (m + n - 1))
return A[i + (k - m)][j + m - 1];
if (k <= (m + n - 1 + m - 1))
return A[i + n - 1][j + m - 1 - (k - (m + n - 1))];
if (k <= (m + n - 1 + m - 1 + n - 2))
return A[i + n - 1 - (k - (m + n - 1 + m - 1))][j + 0];
return findK(A, i + 1, j + 1, n - 2,
m - 2, k - (2 * n + 2 * m - 4));
}
let a = [[ 1, 2, 3, 4, 5, 6 ],
[ 7, 8, 9, 10, 11, 12 ],
[ 13, 14, 15, 16, 17, 18 ]];
let k = 17;
document.write(findK(a, 0, 0, 3, 6, k));
</script>
|
Complexity Analysis:
- Time Complexity: O(c), where c is number of outer circular rings with respect to k’th element.
- Space Complexity: O(1).
As constant space is required.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...