All problems
0213MediumArrayMathRecursionQueueSimulation

Last Apprentice at the Kiln

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1823Find the Winner of the Circular Game

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A pottery studio seats its trainees in a ring around the kiln. There are apprentices of them, numbered 1 upwards going clockwise, so the highest number sits shoulder to shoulder with number 1.

The tutor thins the ring one trainee at a time. Begin at apprentice 1 and count step seats clockwise, counting the apprentice you begin on as one. Whoever you finish on hands over their tools and leaves the ring. Begin the next count from the apprentice sitting immediately clockwise of the one who just left, count step seats again, and carry on the same way. Once the ring has grown short the counting simply keeps going round it, so the same apprentice can be counted more than once inside a single count. When only one apprentice is still seated, that apprentice keeps the wheel for the rest of the day.

Return the seat number of the apprentice who keeps the wheel.

Examples

Example 1

Input
apprentices = 7, step = 3
Output
4

The counts take out apprentices 3, 6, 2, 7, 5 and 1 in that order, and apprentice 4 is the one still seated.

Example 2

Input
apprentices = 9, step = 4
Output
1

The counts clear out apprentices 4, 8, 3, 9, 6, 5, 7 and last of all 2, which leaves apprentice 1 seated.

Example 3

Input
apprentices = 6, step = 1
Output
6

Each count stops on the apprentice it begins with, so seats 1 through 5 clear out in order and apprentice 6 keeps the wheel.

Constraints

  • 1 <= step <= apprentices <= 500

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def last_apprentice_at_the_kiln(apprentices: int, step: int) -> int:
Java
public int lastApprenticeAtTheKiln(int apprentices, int step)
September 7
Apply