Todos los desafíos
Intermedio 0 resueltos 0% de aceptación

Number Spiral Lookup

Find a value by row and column in an expanding number spiral.

The infinite spiral starts at the top-left cell. The first four rows and columns are shown below. Continue the same pattern in expanding square layers. Coordinates start at 1.

 1   2   9  10
 4   3   8  11
 5   6   7  12
16  15  14  13

Return one cell value without constructing the entire grid.

Implement number_spiral(row, column).

Input

row: The row counted from the top, starting at 1.

column: The column counted from the left, starting at 1.

Output

Return the number at that position.

Restricciones

1 ≤ row, column ≤ 1,000,000,000. The value fits a signed 64-bit integer.

Ejemplos

Ejemplo 1
Entrada:
row = 3
column = 2

Salida esperada:
expected = 6
Entrada:
long long row = 3;
long long column = 2;

Salida esperada:
long long expected = 6;
Entrada:
long row = 3;
long column = 2;

Salida esperada:
long expected = 6;

Explicación

In the displayed third row, the second value is 6.

Ejemplo 2
Entrada:
row = 1
column = 4

Salida esperada:
expected = 10
Entrada:
long long row = 1;
long long column = 4;

Salida esperada:
long long expected = 10;
Entrada:
long row = 1;
long column = 4;

Salida esperada:
long expected = 10;

Explicación

The fourth square layer begins at 10 in the top row.

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Aprende de otros

Soluciones de la comunidad

Aún no hay soluciones publicadas. Resuelve este reto para compartir la primera.