The optimal relocation of a tower of n disks between spires takes exactly 2^n - 1 single-disk moves, generated by the standard recursive routine: relocate the top n-1 disks, move the largest disk once, then relocate the top n-1 disks again. Number the disks 1 (smallest) through n (largest).
Given n and a move index m, report which disk is moved on the m-th move (moves are 1-indexed). The intended approach uses the recursive structure: the (2^(n-1))-th move is the one that shifts disk n, and moves before or after it fall inside the two smaller sub-relocations.
A single line with two integers: n m.
A single integer: the number (1..n) of the disk moved on move m.
Example 1
Input
3 4
Expected
3
Explanation
With 3 disks the 4th move is the middle move, which shifts the largest disk, disk 3.
Example 2
Input
3 1
Expected
1
Explanation
The very first move always shifts the smallest disk, disk 1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →