2169. Count Operations to Obtain Zero
UnknownView on LeetCode
Problem Overview
Count Operations to Obtain Zero (Unknown) asks you to solve a structured algorithmic task. This is a common Math / Simulation pattern in coding interviews. Simulate subtraction (equivalent to Euclidean GCD steps); count operations.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Simulate subtraction (equivalent to Euclidean GCD steps); count operations.
Related patterns: Math, Simulation
2169.cs
C#
// Approach: Simulate subtraction (equivalent to Euclidean GCD steps); count operations.
// Time: O(log(min)) Space: O(1)
public class Solution
{
public int CountOperations(int num1, int num2)
{
// Initialize operation counter
int operationCount = 0;
// Continue operations while both numbers are non-zero
while (num1 != 0 && num2 != 0)
{
// Subtract the smaller number from the larger number
if (num1 >= num2)
num1 -= num2;
else
num2 -= num1;
// Increment the operation counter after each subtraction
operationCount++;
}
// Return the total number of operations performed
return operationCount;
}
}Was this solution helpful?
Related Problems
- 592. Fraction Addition and Subtraction(Unknown)
- 885. Spiral Matrix III(Medium)
- 1006. Clumsy Factorial(Medium)
- 1518. Water Bottles(Unknown)
- 1680. Concatenation of Consecutive Binary Numbers(Unknown)
- 1701. Average Waiting Time(Medium)