Ctrl K

Subtract Until a Pile Empties

Easy
There are two piles of coins on the table: a coins in the first and b in the second. In one move, you remove from the larger pile as many coins as the smaller pile holds. If the piles are equal, you take from the first one. The game stops as soon as either pile is empty. Return the number of moves made by then.
Example 1
a=7, b=3 → 5

(7,3) → (4,3) → (1,3) → (1,2) → (1,1) → (0,1): five moves.

Example 2
a=6, b=6 → 1

The piles are equal, so taking 6 from the first one empties it in one move.

Example 3
a=0, b=4 → 0

The first pile is already empty, so no moves are needed.

Constraints
  • 0 ≤ a, b ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSum of Binary Strings EasyFold the Digits Down to One EasyCounting Off by Threes and Fives EasyAdding Long Numbers in Strings EasyGrowing Handouts Around the Circle Easy
loading Python…
Input
a=7, b=3
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 80 tests on the server, hidden ones included.