Count Operations to Obtain Zero (LeetCode 2169): Solution and Explanation
Two non-negative integers keep losing the smaller one from the larger one. Count how many subtractions happen before either number reaches 0.
Intuition
Simulating the rule directly is correct and is fine for small numbers. The trouble is inputs like (100000, 1), where the same subtraction repeats 100,000 times.
Look at what a run of operations does. As long as num1 stays at least num2, every operation subtracts the same num2. That run lasts exactly num1 // num2 operations and leaves num1 % num2 behind. One division replaces the whole run.
A picture makes this concrete. Draw a num1 by num2 rectangle. Each subtraction cuts a square off the long side, and a run of identical subtractions cuts a row of equal squares. The answer is the number of squares that tile the rectangle.
Replacing the larger number with the remainder is the Euclidean algorithm for the greatest common divisor, so the loop runs only O(log) times.
Approach: Euclidean algorithm with quotients
- Set
count = 0. - While both
num1andnum2are non-zero, compare them. - If
num1 >= num2, addnum1 // num2tocountand setnum1 = num1 % num2. - Otherwise add
num2 // num1tocountand setnum2 = num2 % num1. - When a number reaches 0, return
count.
Step-by-step example
num1 = 2, num2 = 3
- Both are non-zero and num2 is larger. 3 // 2 = 1, so
count= 1 and num2 becomes 3 % 2 = 1. - Now (2, 1): num1 is larger. 2 // 1 = 2, so
count= 3 and num1 becomes 2 % 1 = 0. - num1 is 0, so the loop stops and the answer is 3, matching (2,3) -> (2,1) -> (1,1) -> (0,1).
Code: Python, Java, C++
1class Solution:
2 def countOperations(self, num1: int, num2: int) -> int:
3 count = 0
4 while num1 and num2:
5 if num1 >= num2:
6 count += num1 // num2
7 num1 %= num2
8 else:
9 count += num2 // num1
10 num2 %= num1
11 return count1class Solution {
2 public int countOperations(int num1, int num2) {
3 int count = 0;
4 while (num1 != 0 && num2 != 0) {
5 if (num1 >= num2) {
6 count += num1 / num2;
7 num1 %= num2;
8 } else {
9 count += num2 / num1;
10 num2 %= num1;
11 }
12 }
13 return count;
14 }
15}1class Solution {
2public:
3 int countOperations(int num1, int num2) {
4 int count = 0;
5 while (num1 != 0 && num2 != 0) {
6 if (num1 >= num2) {
7 count += num1 / num2;
8 num1 %= num2;
9 } else {
10 count += num2 / num1;
11 num2 %= num1;
12 }
13 }
14 return count;
15 }
16};Time and space complexity
O(log(min(num1, num2)))
Each iteration replaces the larger number with a remainder smaller than the other number, which at least halves the larger number every two steps, as in the Euclidean algorithm.
O(1)
Only the two numbers and the counter are stored.
Other approaches
Direct simulation (time O(max(num1, num2)), space O(1))
Apply the rule one subtraction at a time and count. It matches the statement exactly and passes the given limits, but the step count grows with the ratio of the numbers.
Recursive Euclid (time O(log(min(num1, num2))), space O(log(min(num1, num2))))
Write the same quotient idea recursively: f(a, b) = a // b + f(b, a % b) with f(a, 0) = 0, after ordering so a >= b. It is equally fast and reads like the GCD recursion.
Common mistakes
- Forgetting that either input can already be 0. The loop must not run, and dividing by zero must never happen.
- Adding 1 per iteration instead of the quotient. Each iteration of the fast loop stands for many subtractions.
- Swapping the numbers to keep num1 larger and then mixing up which one gets the remainder. Either keep the two branches or swap consistently.
- Treating equal numbers specially. When num1 == num2 the quotient is 1 and the remainder is 0, which the general branch already handles.
Frequently asked questions
Why does num1 // num2 count the subtractions correctly?
While num1 >= num2 the same num2 is subtracted each time, and num2 does not change during that run. The number of times you can subtract num2 before num1 drops below it is exactly the integer quotient.
Is the plain simulation fast enough?
With values up to 100,000 it does at most 100,000 steps, which passes. The division version is what you would use for larger limits and is what interviewers look for.
How is this related to the GCD?
The subtraction rule is the original form of Euclid's algorithm. The last non-zero number left is gcd(num1, num2), and the count is the sum of the quotients in the continued fraction of num1 / num2.