Given two integers **x** and **y **representing the row and column number in a matrix respectively, the task is to find the integer at the cell **(x, y) **in the Spirally Filled Matrix.

Explanation:

The size of the matrix change according to the insertion of elements.

As the row and columns are filled layer – by – layer, a square matrix will be formed as each row and column are added simultaneously in the following manner:

- A square matrix of size 1 x 1 is formed having a single element 1.
- A square matrix of size 2 x 2 is formed after spiral insertion of elements {2, 3, 4} in order:

{{1, 2},

{4, 3}}- A square matrix of size 3 x 3 is formed after spiral insertion of elements {5, 6, 7, 8} in order:

{{1, 2, 9},

{4, 3, 8},

{5, 6, 7}}

Below image denotes a 4*4 matrix generated following the above steps:

**Examples:**

Input:X = 2, Y = 3Output:8Explanation:

The element at row 2 and column 3 is 8.Input:X = 5, Y = 4Output:20Explanation:The element at row 5 and column 4 is 20.

**Approach:**

To solve the problem, the logic behind the given spirally filled matrix needs to be understood. Following are the possible cases that needs to be considered:

**Case 1: If y > x & y is odd**

The element at**(x, y)**is equal to**y**^{2}– x +1

Illustration:

y = 3, x = 1

y^{2}– x +1 = 9 – 1 + 1 = 9

Element present at (1, 3) = 9.

**Case 2: If y > x & y is even**

The element at**(x, y)**is equal to**(y – 1)**^{2}+ x

**Case 3: If x ? y & x is even**

The element at**(x, y)**is equal to**x**^{2}– y +1

**Case 4: If If x ? y & x is odd**

The element at**(x, y)**is equal to**(x – 1)**^{2}+ y

- Search element in a Spirally sorted Matrix
- Kth Smallest Element of a Matrix of given dimensions filled with product of indices
- Number of ways of cutting a Matrix such that atleast one cell is filled in each part
- Count 1s in binary matrix having remaining indices of its row and column filled with 0s
- Find all rectangles filled with 0
- Count of contiguous subarrays possible for every index by including the element at that index
- Program to check if tank will overflow, underflow or filled in given time
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Maximize count of empty water bottles from N filled bottles
- Number of containers that can be filled in the given time
- Count of vessels completely filled after a given time
- Maximum strength in a Matrix after performing specified operations
- Element in a matrix starting from which anti-clockwise traversal ends at the last element
- Print index of columns sorted by count of zeroes in the Given Matrix
- Minimize steps required to move all 1's in a matrix to a given index
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Rearrange and update array elements as specified by the given queries
- Count of distinct index pair (i, j) such that element sum of First Array is greater
- Maximum subsequence sum possible by multiplying each element by its index
- Find a common element in all rows of a given row-wise sorted matrix

Illustration:

y = 4, x = 1

(y – 1)^{2}+ x = 9 + 1 = 10

Element present at (1, 4) = 10.

Illustration:

y = 1, x = 4

x^{2}– y + 1 = 16 – 1 + 1 = 16

Element present at (4, 1) = 16.

Illustration:

y = 2, x = 3

(x – 1)^{2}+ y = 4 + 2 = 6

Element present at (3, 2) = 6.

Hence, in order to solve the problem, we need to evaluate and print the result of the equation corresponding to the condition which the given **x** and **y** satisfies.

Below is the implementation of the above approach:

## C++

`// C++ Program to find the element ` `// at given position in spirally ` `// filled matrix ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the ` `// element at (x, y) ` `int` `SpiralElement(` `int` `x, ` `int` `y) ` `{ ` ` ` `int` `r; ` ` ` ` ` `// If y is greater ` ` ` `if` `(x < y) { ` ` ` ` ` `// If y is odd ` ` ` `if` `(y % 2 == 1) { ` ` ` `r = y * y; ` ` ` `return` `(r - x + 1); ` ` ` `} ` ` ` ` ` `// If y is even ` ` ` `else` `{ ` ` ` `r = (y - 1) * (y - 1); ` ` ` `return` `(r + x); ` ` ` `} ` ` ` `} ` ` ` ` ` `else` `{ ` ` ` ` ` `// If x is even ` ` ` `if` `(x % 2 == 0) { ` ` ` `r = x * x; ` ` ` `return` `(r - y + 1); ` ` ` `} ` ` ` ` ` `// If x is odd ` ` ` `else` `{ ` ` ` `r = (x - 1) * (x - 1); ` ` ` `return` `(r + y); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `x = 2, y = 3; ` ` ` `cout << SpiralElement(x, y); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the element ` `// at given position in spirally ` `// filled matrix ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to return the ` `// element at (x, y) ` `static` `int` `SpiralElement(` `int` `x, ` `int` `y) ` `{ ` ` ` `int` `r; ` ` ` ` ` `// If y is greater ` ` ` `if` `(x < y) ` ` ` `{ ` ` ` ` ` `// If y is odd ` ` ` `if` `(y % ` `2` `== ` `1` `) ` ` ` `{ ` ` ` `r = y * y; ` ` ` `return` `(r - x + ` `1` `); ` ` ` `} ` ` ` ` ` `// If y is even ` ` ` `else` ` ` `{ ` ` ` `r = (y - ` `1` `) * (y - ` `1` `); ` ` ` `return` `(r + x); ` ` ` `} ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` ` ` `// If x is even ` ` ` `if` `(x % ` `2` `== ` `0` `) ` ` ` `{ ` ` ` `r = x * x; ` ` ` `return` `(r - y + ` `1` `); ` ` ` `} ` ` ` ` ` `// If x is odd ` ` ` `else` ` ` `{ ` ` ` `r = (x - ` `1` `) * (x - ` `1` `); ` ` ` `return` `(r + y); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `x = ` `2` `, y = ` `3` `; ` ` ` ` ` `System.out.println(SpiralElement(x, y)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

**Output:**

8

**Time complexity:** O(1) **Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.