Skip to content

Commit b524ef7

Browse files
Added task 2169.
1 parent 941bb41 commit b524ef7

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package g2101_2200.s2169_count_operations_to_obtain_zero;
2+
3+
// #Easy #Math #Simulation #2022_06_07_Time_0_ms_(100.00%)_Space_39_MB_(96.68%)
4+
5+
public class Solution {
6+
public int countOperations(int num1, int num2) {
7+
int ans = 0;
8+
while ((num1 * num2) != 0) {
9+
if (num1 >= num2) {
10+
ans += num1 / num2;
11+
num1 = num1 % num2;
12+
} else {
13+
ans += num2 / num1;
14+
num2 = num2 % num1;
15+
}
16+
}
17+
return ans;
18+
}
19+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2169\. Count Operations to Obtain Zero
2+
3+
Easy
4+
5+
You are given two **non-negative** integers `num1` and `num2`.
6+
7+
In one **operation**, if `num1 >= num2`, you must subtract `num2` from `num1`, otherwise subtract `num1` from `num2`.
8+
9+
* For example, if `num1 = 5` and `num2 = 4`, subtract `num2` from `num1`, thus obtaining `num1 = 1` and `num2 = 4`. However, if `num1 = 4` and `num2 = 5`, after one operation, `num1 = 4` and `num2 = 1`.
10+
11+
Return _the **number of operations** required to make either_ `num1 = 0` _or_ `num2 = 0`.
12+
13+
**Example 1:**
14+
15+
**Input:** num1 = 2, num2 = 3
16+
17+
**Output:** 3
18+
19+
**Explanation:**
20+
21+
- Operation 1: num1 = 2, num2 = 3. Since num1 < num2, we subtract num1 from num2 and get num1 = 2, num2 = 3 - 2 = 1.
22+
23+
- Operation 2: num1 = 2, num2 = 1. Since num1 > num2, we subtract num2 from num1.
24+
25+
- Operation 3: num1 = 1, num2 = 1. Since num1 == num2, we subtract num2 from num1.
26+
27+
Now num1 = 0 and num2 = 1. Since num1 == 0, we do not need to perform any further operations.
28+
29+
So the total number of operations required is 3.
30+
31+
**Example 2:**
32+
33+
**Input:** num1 = 10, num2 = 10
34+
35+
**Output:** 1
36+
37+
**Explanation:**
38+
39+
- Operation 1: num1 = 10, num2 = 10. Since num1 == num2, we subtract num2 from num1 and get num1 = 10 - 10 = 0.
40+
41+
Now num1 = 0 and num2 = 10. Since num1 == 0, we are done.
42+
43+
So the total number of operations required is 1.
44+
45+
**Constraints:**
46+
47+
* <code>0 <= num1, num2 <= 10<sup>5</sup></code>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2101_2200.s2169_count_operations_to_obtain_zero;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void countOperations() {
11+
assertThat(new Solution().countOperations(2, 3), equalTo(3));
12+
}
13+
14+
@Test
15+
void countOperations2() {
16+
assertThat(new Solution().countOperations(10, 10), equalTo(1));
17+
}
18+
}

0 commit comments

Comments
 (0)