Count Operations to Obtain Zero - Problem
You're given two non-negative integers num1 and num2. Your task is to perform a series of subtraction operations until one of the numbers becomes zero.
Operation Rule:
- If
num1 >= num2, subtractnum2fromnum1 - Otherwise, subtract
num1fromnum2
Example: Starting with num1 = 5 and num2 = 4:
- Since 5 โฅ 4, subtract:
num1 = 5 - 4 = 1,num2 = 4 - Since 1 < 4, subtract:
num1 = 1,num2 = 4 - 1 = 3 - Continue until one becomes zero...
Return the total number of operations needed.
Input & Output
example_1.py โ Basic Case
$
Input:
num1 = 2, num2 = 3
โบ
Output:
3
๐ก Note:
Operation 1: 2 < 3, so num2 = 3 - 2 = 1. Operation 2: 2 >= 1, so num1 = 2 - 1 = 1. Operation 3: 1 >= 1, so num1 = 1 - 1 = 0. Total: 3 operations.
example_2.py โ Larger Numbers
$
Input:
num1 = 10, num2 = 10
โบ
Output:
1
๐ก Note:
Since both numbers are equal, one operation makes num1 = 10 - 10 = 0. Total: 1 operation.
example_3.py โ Edge Case
$
Input:
num1 = 1, num2 = 1
โบ
Output:
1
๐ก Note:
Both numbers are 1, so after one subtraction: num1 = 1 - 1 = 0. Total: 1 operation.
Constraints
- 0 โค num1, num2 โค 106
- At least one of num1 or num2 will be greater than 0
- The answer will fit in a 32-bit signed integer
Visualization
Tap to expand
Understanding the Visualization
1
Setup
Start with two piles: num1 stones and num2 stones
2
Compare
Identify which pile has more stones
3
Subtract
Remove smaller pile's count from larger pile
4
Repeat
Continue until one pile becomes empty
5
Optimize
Use division to skip multiple identical operations
Key Takeaway
๐ฏ Key Insight: This problem is essentially the Euclidean algorithm for finding GCD, but counting steps instead of finding the result!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code