Two experimental reactor cores aboard a research vessel hold non-negative integer charge levels c1 and c2, measured in mega-joules. While both cores still hold a positive charge, the safety system performs a drain operation: the core with the larger (or equal) charge instantly drains an amount equal to the OTHER core's current charge -- that is, if c1 >= c2 then c1 decreases by c2, otherwise c2 decreases by c1. The system keeps performing drain operations, one at a time, until at least one of the two cores reaches exactly zero charge. Given the two starting charge levels, determine how many drain operations are performed in total.
A single line containing two integers c1 and c2, separated by a space.
A single integer: the total number of drain operations performed until c1 or c2 becomes zero.
Example 1
Input
2 3
Expected
3
Explanation
Start (2,3). c1 < c2, so c2 -= c1 -> (2,1) [operation 1]. Now c1 >= c2, so c1 -= c2 -> (1,1) [operation 2]. Now c1 >= c2 again, so c1 -= c2 -> (0,1) [operation 3]. c1 is now 0, so we stop. Total operations = 3.
Example 2
Input
10 10
Expected
1
Explanation
Start (10,10). c1 >= c2, so c1 -= c2 -> (0,10) [operation 1]. c1 is now 0, so we stop immediately. Total operations = 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 →