#2169
Easy Algorithms Count operations to obtain zero
Math Simulation
79.8% acceptance
Feb 25, 2026
915
36
You are given two non-negative integers num1 and num2.
In one operation, if num1 >= num2, subtract num2 from num1, else subtract num1 from num2.
Return the number of operations required to make either num1 = 0 or num2 = 0.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn count_operations(mut num1: i32, mut num2: i32) -> i32 {
let mut ops = 0;
while num1 > 0 && num2 > 0 {
if num1 >= num2 {
ops += num1 / num2;
num1 %= num2;
} else {
ops += num2 / num1;
num2 %= num1;
}
}
ops
}
}