Simple Simulation Approach- O(n × m) Time and O(1) Space
The idea is to traverse the matrix in spiral order starting from the top-left corner and maintain a counter to track the number of elements visited. A variable count is initialized to 0, and it is incremented each time an element is visited during the traversal. As soon as count becomes equal to k, the current element is returned as the answer. The matrix is traversed layer by layer in spiral form, ensuring all elements are visited in the correct order until the k-th element is found.
C++
#include<iostream>#include<vector>usingnamespacestd;// finds kth element in spiral traversalintfindK(vector<vector<int>>&mat,intk){intm=mat.size();intn=mat[0].size();intcount=0;intlast=-1;inttop=0,bottom=m-1;intleft=0,right=n-1;while(top<=bottom&&left<=right&&count<k){// top rowfor(inti=left;i<=right&&count<k;i++){last=mat[top][i];count++;}top++;// right columnfor(inti=top;i<=bottom&&count<k;i++){last=mat[i][right];count++;}right--;// bottom rowif(top<=bottom){for(inti=right;i>=left&&count<k;i--){last=mat[bottom][i];count++;}bottom--;}// left columnif(left<=right){for(inti=bottom;i>=top&&count<k;i--){last=mat[i][left];count++;}left++;}}returnlast;}intmain(){vector<vector<int>>mat={{1,2,3},{4,5,6},{7,8,9}};intk=4;cout<<findK(mat,k);return0;}
Java
classGFG{// finds kth element in spiral traversalpublicstaticintfindK(int[][]mat,intk){intm=mat.length;intn=mat[0].length;inttop=0,bottom=m-1;intleft=0,right=n-1;intcount=0,last=-1;while(top<=bottom&&left<=right&&count<k){// top rowfor(inti=left;i<=right&&count<k;i++){last=mat[top][i];count++;}top++;// right columnfor(inti=top;i<=bottom&&count<k;i++){last=mat[i][right];count++;}right--;// bottom rowif(top<=bottom){for(inti=right;i>=left&&count<k;i--){last=mat[bottom][i];count++;}bottom--;}// left columnif(left<=right){for(inti=bottom;i>=top&&count<k;i--){last=mat[i][left];count++;}left++;}}returnlast;}publicstaticvoidmain(String[]args){int[][]mat={{1,2,3},{4,5,6},{7,8,9}};intk=4;System.out.println(findK(mat,k));}}
Python
# finds kth element in spiral traversaldeffindK(mat,k):m=len(mat)n=len(mat[0])top,bottom=0,m-1left,right=0,n-1count=0last=-1whiletop<=bottomandleft<=rightandcount<k:# top rowforiinrange(left,right+1):ifcount==k:returnlastlast=mat[top][i]count+=1top+=1# right columnforiinrange(top,bottom+1):ifcount==k:returnlastlast=mat[i][right]count+=1right-=1# bottom rowiftop<=bottom:foriinrange(right,left-1,-1):ifcount==k:returnlastlast=mat[bottom][i]count+=1bottom-=1# left columnifleft<=right:foriinrange(bottom,top-1,-1):ifcount==k:returnlastlast=mat[i][left]count+=1left+=1returnlastif__name__=="__main__":mat=[[1,2,3],[4,5,6],[7,8,9]]k=4print(findK(mat,k))
C#
usingSystem;usingSystem.Collections.Generic;classGFG{// finds kth element in spiral traversalpublicstaticintfindK(List<List<int>>mat,intk){intm=mat.Count;intn=mat[0].Count;inttop=0,bottom=m-1;intleft=0,right=n-1;intcount=0;intlast=-1;while(top<=bottom&&left<=right&&count<k){// top rowfor(inti=left;i<=right&&count<k;i++){last=mat[top][i];count++;}top++;// right columnfor(inti=top;i<=bottom&&count<k;i++){last=mat[i][right];count++;}right--;// bottom rowif(top<=bottom){for(inti=right;i>=left&&count<k;i--){last=mat[bottom][i];count++;}bottom--;}// left columnif(left<=right){for(inti=bottom;i>=top&&count<k;i--){last=mat[i][left];count++;}left++;}}returnlast;}publicstaticvoidMain(){List<List<int>>mat=newList<List<int>>{newList<int>{1,2,3},newList<int>{4,5,6},newList<int>{7,8,9}};intk=4;Console.WriteLine(findK(mat,k));}}
JavaScript
// finds kth element in spiral traversalfunctionfindK(mat,k){letm=mat.length;letn=mat[0].length;letlast=-1;letcount=0;lettop=0,bottom=m-1,left=0,right=n-1;while(top<=bottom&&left<=right&&count<k){// top rowfor(leti=left;i<=right&&count<k;i++){last=mat[top][i];count++;}top++;// right columnfor(leti=top;i<=bottom&&count<k;i++){last=mat[i][right];count++;}right--;// bottom row if(top<=bottom){for(leti=right;i>=left&&count<k;i--){last=mat[bottom][i];count++;}bottom--;}// left columnif(left<=right){for(leti=bottom;i>=top&&count<k;i--){last=mat[i][left];count++;}left++;}}returnlast;}// Driver codeletmat=[[1,2,3],[4,5,6],[7,8,9]];letk=6;console.log(findK(mat,k));
Output
6
Optimized Layer- wise Traversal - O(max(n, m)) Time and O(max(n, m)) Space
The idea is to observe that spiral traversal proceeds in layers (or cycles). Each layer of the matrix consists of four sides: top row, right column, bottom row, and left column. Instead of traversing element by element, we can directly determine the position of the k-th element within a layer.
For a matrix of size m × n, a cycle can be divided into four parts:
If k ≤ m, the element lies in the first row
Else if k ≤ (m + n - 1), the element lies in the last column
Else if k ≤ (m + n - 1 + m - 1), the element lies in the last row
Else if k ≤ (m + n - 1 + m - 1 + n - 2), the element lies in the first column
If the k-th element is found in any of the above cases, it can be computed in constant time. Otherwise, we remove the outer cycle, reduce the matrix size, adjust the value of k, and recursively repeat the process until the element is found.
C++
#include<iostream>#include<vector>usingnamespacestd;intfindKUtil(vector<vector<int>>&mat,inti,intj,intn,intm,intk){if(n<1||m<1)return-1;// If element is in outermost ring// Element is in first rowif(k<=m)returnmat[i][j+k-1];// Element is in last columnif(k<=(m+n-1))returnmat[i+(k-m)][j+m-1];// Element is in last rowif(k<=(m+n-1+m-1))returnmat[i+n-1][j+m-1-(k-(m+n-1))];// Element is in first columnif(k<=(m+n-1+m-1+n-2))returnmat[i+n-1-(k-(m+n-1+m-1))][j];returnfindKUtil(mat,i+1,j+1,n-2,m-2,k-(2*n+2*m-4));}intfindK(vector<vector<int>>&mat,intk){intn=mat.size();intm=mat[0].size();returnfindKUtil(mat,0,0,n,m,k);}intmain(){vector<vector<int>>mat={{1,2,3},{4,5,6},{7,8,9}};intk=4;cout<<findK(mat,k);return0;}
Java
classGfG{staticintfindKUtil(int[][]mat,inti,intj,intn,intm,intk){if(n<1||m<1)return-1;// First rowif(k<=m)returnmat[i][j+k-1];// Last columnif(k<=m+n-1)returnmat[i+(k-m)][j+m-1];// Last rowif(k<=m+n-1+m-1)returnmat[i+n-1][j+m-1-(k-(m+n-1))];// First columnif(k<=m+n-1+m-1+n-2)returnmat[i+n-1-(k-(m+n-1+m-1))][j];// Move to inner matrixreturnfindKUtil(mat,i+1,j+1,n-2,m-2,k-(2*n+2*m-4));}staticintfindK(int[][]mat,intk){intn=mat.length;intm=mat[0].length;returnfindKUtil(mat,0,0,n,m,k);}publicstaticvoidmain(String[]args){int[][]mat={{1,2,3},{4,5,6},{7,8,9}};intk=4;System.out.println(findK(mat,k));// Output: 6}}
Python
deffindKUtil(mat,i,j,n,m,k):ifn<1orm<1:return-1# If element is in outermost ring# Element is in first rowifk<=m:returnmat[i+0][j+k-1]# Element is in last columnifk<=(m+n-1):returnmat[i+(k-m)][j+m-1]# Element is in last rowifk<=(m+n-1+m-1):returnmat[i+n-1][j+m-1-(k-(m+n-1))]# Element is in first columnifk<=(m+n-1+m-1+n-2):returnmat[i+n-1-(k-(m+n-1+m-1))][j+0]returnfindKUtil(mat,i+1,j+1,n-2,m-2,k-(2*n+2*m-4))deffindK(mat,k):n=len(mat)m=len(mat[0])returnfindKUtil(mat,0,0,n,m,k)if__name__=="__main__":mat=[[1,2,3],[4,5,6],[7,8,9]]k=4print(findK(mat,k))
C#
usingSystem;usingSystem.Collections.Generic;classGfG{staticintfindKUtil(List<List<int>>mat,inti,intj,intn,intm,intk){if(n<1||m<1)return-1;// If element is in outermost ring// Element is in first rowif(k<=m)returnmat[i][j+k-1];// Element is in last columnif(k<=(m+n-1))returnmat[i+(k-m)][j+m-1];// Element is in last rowif(k<=(m+n-1+m-1))returnmat[i+n-1][j+m-1-(k-(m+n-1))];// Element is in first columnif(k<=(m+n-1+m-1+n-2))returnmat[i+n-1-(k-(m+n-1+m-1))][j];returnfindKUtil(mat,i+1,j+1,n-2,m-2,k-(2*n+2*m-4));}staticintfindK(List<List<int>>mat,intk){intn=mat.Count;intm=mat[0].Count;returnfindKUtil(mat,0,0,n,m,k);}publicstaticvoidMain(){List<List<int>>mat=newList<List<int>>{newList<int>{1,2,3},newList<int>{4,5,6},newList<int>{7,8,9}};intk=4;Console.WriteLine(findK(mat,k));}}
JavaScript
functionfindKUtil(mat,i,j,n,m,k){if(n<1||m<1)return-1;// If element is in outermost ring// Element is in first rowif(k<=m)returnmat[i][j+k-1];// Element is in last columnif(k<=m+n-1)returnmat[i+(k-m)][j+m-1];// Element is in last rowif(k<=m+n-1+m-1)returnmat[i+n-1][j+m-1-(k-(m+n-1))];// Element is in first columnif(k<=m+n-1+m-1+n-2)returnmat[i+n-1-(k-(m+n-1+m-1))][j];returnfindKUtil(mat,i+1,j+1,n-2,m-2,k-(2*n+2*m-4));}functionfindK(mat,k){letn=mat.length;letm=mat[0].length;returnfindKUtil(mat,0,0,n,m,k);}// Driver codeletmat=[[1,2,3],[4,5,6],[7,8,9]];letk=4;console.log(findK(mat,k));