LeetCode Challenge Day 55 β 2169. Count Operations to Obtain Zero
Nitin Ahirwal / November 9, 2025
Hey folks π
This is Day 55 of my LeetCode streak π
Todayβs problem is 2169. Count Operations to Obtain Zero β an easy yet elegant math-based problem that builds directly on the Euclidean algorithm logic for finding GCDs, but with a twist β instead of returning the GCD, we count the total number of subtraction operations.
π Problem Statement
You are given two non-negative integers num1 and num2.
In one operation:
- If
num1 >= num2, you must subtractnum2fromnum1. - Otherwise, subtract
num1fromnum2.
Return the number of operations required until either num1 or num2 becomes 0.
Example 1:
Input: num1 = 2, num2 = 3
Output: 3
Explanation:
Operation 1 β num2 = 3 - 2 = 1
Operation 2 β num1 = 2 - 1 = 1
Operation 3 β num2 = 1 - 1 = 0
Example 2:
Input: num1 = 10, num2 = 10
Output: 1
Explanation: num1 = num2 = 10 β subtract once β (0, 10)
π‘ Intuition
At first glance, this looks like a repeated subtraction process β but thereβs a pattern.
The problem mirrors the Euclidean GCD algorithm, where instead of computing the GCD, we count how many subtractions it would take to reduce both numbers to zero.
Each subtraction operation group can be optimized:
- Instead of subtracting one by one, we can perform
Math.floor(num1 / num2)operations in a single step β since thatβs how many timesnum2fits insidenum1.
π Approach
- Initialize a
countvariable to store the total number of subtraction operations. - While both
num1andnum2are greater than zero:- If
num1 >= num2, incrementcountbyMath.floor(num1 / num2)and updatenum1 = num1 % num2. - Otherwise, increment
countbyMath.floor(num2 / num1)and updatenum2 = num2 % num1.
- If
- Once one number becomes zero, exit the loop.
- Return the
count.
This approach effectively simulates all subtraction steps without actually performing each one individually.
β±οΈ Complexity Analysis
-
Time Complexity:
( O(\log(\min(num1, num2))) ) β
The process mimics the Euclidean algorithm for GCD which operates in logarithmic time. -
Space Complexity:
( O(1) ) β
We only use a constant number of variables.
π§βπ» Code (JavaScript)
/**
* @param {number} num1
* @param {number} num2
* @return {number}
*/
var countOperations = function(num1, num2) {
let count = 0;
while (num1 !== 0 && num2 !== 0) {
if (num1 >= num2) {
count += Math.floor(num1 / num2);
num1 = num1 % num2;
} else {
count += Math.floor(num2 / num1);
num2 = num2 % num1;
}
}
return count;
};
π§© Example Walkthrough
Input:
num1 = 5, num2 = 4
Process:
Step 1 β 5 >= 4 β count += 1 β num1 = 1, num2 = 4
Step 2 β 4 >= 1 β count += 4 β num2 = 0
Total count = 5
Output:
5
π― Reflection
This problem is a great demonstration of how mathematical reasoning and optimization can simplify what seems like a loop-heavy process. By thinking in terms of division and modulus instead of repetitive subtraction, we achieve an efficient and elegant solution.
Thatβs it for Day 55 of my LeetCode challenge πͺ Keep grinding and optimizing your thought process β one operation at a time β‘
Happy Coding π¨βπ»