Find nth element of spiral matrix
Submissions: 3318   Accuracy:


  Difficulty: Easy   Marks: 2

Given a matrix your task is to find the kth element which is obtained while traversing the matrix spirally. You need to complete the method findK which takes four arguments the first argument is the matrix A and the next two arguments will be n and m denoting the size of the matrix A and then the forth argument is an integer  k denoting the kth element . The function will return the kth element obtained while traversing the matrix spirally.

The first line of input is the no of test cases then T test cases follow . The first line of each test case has three integers n(rows), m(columns) and k . Then in the next line are n*m space separated values of the matrix A [ ] [ ] .

The output for each test case will be the kth obtained element .



3 3 4
1 2 3 4 5 6 7 8 9 


The matrix above will look like 
1 2 3
4 5 6
7 8 9
and the 4th element in spiral fashion will be 6 .

Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Amit Khandelwal, Harshit Sidhwa
Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use, generate link and share the link here.

to report an issue on this page.