2169. Count Operations to Obtain Zero
Difficulty: Easy
Topics: Math, Simulation, Weekly Contest 280
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 = 5andnum2 = 4, subtractnum2fromnum1, thus obtainingnum1 = 1andnum2 = 4. However, ifnum1 = 4andnum2 = 5, after one operation,num1 = 4andnum2 = 1.
Return the number of operations required to make either num1 = 0 or num2 = 0.
Example 1:
- Input: num1 = 2, num2 = 3
- Output: 3
- Explanation:
- Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
- Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1.
- Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.
- Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.
- So the total number of operations required is 3.
Example 2:
- Input: num1 = 10, num2 = 10
- Output: 1
- Explanation:
- Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0.
- Now num1 = 0 and num2 = 10. Since num1 == 0, we are done.
- So the total number of operations required is 1.
Constraints:
0 <= num1, num2 <= 10⁵
Hint:
- Try simulating the process until either of the two integers is zero.
- Count the number of operations done.
Solution:
We need to determine the number of operations required to reduce either of two given non-negative integers to zero by repeatedly subtracting the smaller number from the larger one. The operations continue until one of the numbers becomes zero.
Approach
- Simulation with Optimization: Instead of simulating each subtraction step one by one (which could be inefficient for large numbers), we use integer division to calculate how many operations can be performed in one go.
- Efficient Subtraction: For each step, if
num1is greater than or equal tonum2, we calculate how many timesnum2can be subtracted fromnum1using integer division. This count is added to the total operations, andnum1is updated to the remainder ofnum1divided bynum2. Similarly, ifnum2is larger, we do the same fornum2usingnum1. - Termination Check: The loop continues until either
num1ornum2becomes zero. The total count of operations performed is then returned.
Let's implement this solution in PHP: 2169. Count Operations to Obtain Zero
<?php /** * @param Integer $num1 * @param Integer $num2 * @return Integer */ function countOperations($num1, $num2) { ... ... ... /** * go to ./solution.php */ } // Test cases echo countOperations(2, 3) . PHP_EOL; // Output: 3 echo countOperations(10, 10) . PHP_EOL; // Output: 1 ?> Explanation:
- Initialization: We start with a count of zero operations.
- Loop Execution: The loop runs as long as both numbers are non-zero. In each iteration:
- If
num1is greater than or equal tonum2, we compute how many timesnum2can be subtracted fromnum1using integer division. This quotient is added to the count, andnum1is updated to the remainder ofnum1divided bynum2. - If
num2is greater thannum1, we compute how many timesnum1can be subtracted fromnum2similarly, update the count, and setnum2to the remainder.
- If
- Termination: Once either
num1ornum2becomes zero, the loop exits, and the total count of operations is returned.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)