Count Operations to Obtain Zero

You are given two non-negative integers num1 and num2. In one operation, if num1 >= num2 you subtract num2 from num1, otherwise you subtract num1 from num2. Return the number of operations needed until num1 = 0 or num2 = 0.

30:00

Count Operations to Obtain Zero
easy
Free written solution
Topics
Companies

You are given two non-negative integers num1 and num2. In one operation, if num1 >= num2 you subtract num2 from num1, otherwise you subtract num1 from num2. Return the number of operations needed until num1 = 0 or num2 = 0.

Example 1:
Input: {"num1":2,"num2":3}
Output: 3
Constraints:
  • 0≤num1,num2≤1050 \leq \text{num1}, \text{num2} \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"num1":2,"num2":3}

Draw a 2 by 3 rectangle. One operation subtracts the smaller side from the larger, which is the same as cutting a square off the rectangle.

num1 (width)

2

num2 (height)

3

count

0

2 × 3 rectangle, each square = one subtraction

cuts so far (one loop iteration each)

none yet

Variables
VariableValue
num12
num23
count0
DepthFunction Call
Stack empty
0/6