Count Operations to Obtain Zero - Problem
You are given two non-negative integers num1 and num2.
In one operation, if num1 >= num2, you must subtract num2 from num1, otherwise subtract num1 from num2.
For example, if num1 = 5 and num2 = 4, subtract num2 from num1, thus obtaining num1 = 1 and num2 = 4. However, if num1 = 4 and num2 = 5, after one operation, num1 = 4 and num2 = 1.
Return the number of operations required to make either num1 = 0 or num2 = 0.
Input & Output
Example 1 — Basic Case
$
Input:
num1 = 2, num2 = 3
›
Output:
3
💡 Note:
Operation 1: 3 ≥ 2, so num2 = 3 - 2 = 1, now num1 = 2, num2 = 1. Operation 2: 2 ≥ 1, so num1 = 2 - 1 = 1, now num1 = 1, num2 = 1. Operation 3: 1 = 1, so num1 = 1 - 1 = 0. Total: 3 operations.
Example 2 — One Operation
$
Input:
num1 = 10, num2 = 10
›
Output:
1
💡 Note:
Since both numbers are equal, one operation makes one of them zero: 10 - 10 = 0. Total: 1 operation.
Example 3 — Multiple Operations
$
Input:
num1 = 1, num2 = 5
›
Output:
5
💡 Note:
5-1=4, 4-1=3, 3-1=2, 2-1=1, 1-1=0. Total: 5 operations to make num1 zero.
Constraints
- 0 ≤ num1, num2 ≤ 106
Visualization
Tap to expand
Understanding the Visualization
1
Input
Two non-negative integers num1 and num2
2
Process
Keep subtracting smaller from larger until one becomes zero
3
Output
Total number of operations performed
Key Takeaway
🎯 Key Insight: This is the Euclidean algorithm in disguise - use division to optimize!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code