FunctionRacerProblemsRace

Square on a Winding Board

BeginnerGridsPython, JavaScript, TypeScript, Java, C++

A board game has rows rows and cols columns of squares, numbered from 1 to rows * cols along a winding path. Square 1 is in the bottom-left corner. The numbers run from left to right along the bottom row, continue from right to left along the row above it, then from left to right along the next row up, and so on, switching direction on every row. Return the position of square number square as [row, col], where row 0 is the top row of the board and column 0 is the leftmost column.

Example 1

rows 3
cols 4
square 6
returns [1,2]

The bottom row (row 2) holds 1 to 4 from left to right and the middle row holds 5 to 8 from right to left, so 6 sits in row 1, column 2.

Example 2

rows 3
cols 4
square 12
returns [0,3]

The top row runs from left to right again with 9 to 12, so 12 is at its right end.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Grids problems