Back to posts

LeetCode Challenge Day 55 β€” 2169. Count Operations to Obtain Zero

Nitin Ahirwal / November 9, 2025

LeetCode ChallengeDay 55MathematicsGCDSubtractionJavaScriptAlgorithmEasyEuclidean Algorithm

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 subtract num2 from num1.
  • Otherwise, subtract num1 from num2.

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 times num2 fits inside num1.

πŸ”‘ Approach

  1. Initialize a count variable to store the total number of subtraction operations.
  2. While both num1 and num2 are greater than zero:
    • If num1 >= num2, increment count by Math.floor(num1 / num2) and update num1 = num1 % num2.
    • Otherwise, increment count by Math.floor(num2 / num1) and update num2 = num2 % num1.
  3. Once one number becomes zero, exit the loop.
  4. 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 πŸ‘¨β€πŸ’»