Skip to content

Commit 3fcb675

Browse files
Added task 2154.
1 parent 4809b72 commit 3fcb675

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2101_2200.s2154_keep_multiplying_found_values_by_two;
2+
3+
// #Easy #Array #Hash_Table #Sorting #Simulation
4+
// #2022_06_01_Time_1_ms_(93.21%)_Space_44.6_MB_(46.46%)
5+
6+
public class Solution {
7+
public int findFinalValue(int[] nums, int original) {
8+
int i = 0;
9+
while (i < nums.length) {
10+
if (nums[i] == original) {
11+
original = original * 2;
12+
i = -1;
13+
}
14+
i++;
15+
}
16+
return original;
17+
}
18+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2154\. Keep Multiplying Found Values by Two
2+
3+
Easy
4+
5+
You are given an array of integers `nums`. You are also given an integer `original` which is the first number that needs to be searched for in `nums`.
6+
7+
You then do the following steps:
8+
9+
1. If `original` is found in `nums`, **multiply** it by two (i.e., set `original = 2 * original`).
10+
2. Otherwise, **stop** the process.
11+
3. **Repeat** this process with the new number as long as you keep finding the number.
12+
13+
Return _the **final** value of_ `original`.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [5,3,6,1,12], original = 3
18+
19+
**Output:** 24
20+
21+
**Explanation:**
22+
- 3 is found in nums. 3 is multiplied by 2 to obtain 6.
23+
24+
- 6 is found in nums. 6 is multiplied by 2 to obtain 12.
25+
26+
- 12 is found in nums. 12 is multiplied by 2 to obtain 24.
27+
28+
- 24 is not found in nums.
29+
Thus, 24 is returned.
30+
31+
**Example 2:**
32+
33+
**Input:** nums = [2,7,9], original = 4
34+
35+
**Output:** 4
36+
37+
**Explanation:**
38+
- 4 is not found in nums.
39+
40+
Thus, 4 is returned.
41+
42+
**Constraints:**
43+
44+
* `1 <= nums.length <= 1000`
45+
* `1 <= nums[i], original <= 1000`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g2101_2200.s2154_keep_multiplying_found_values_by_two;
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 findFinalValue() {
11+
assertThat(new Solution().findFinalValue(new int[] {5, 3, 6, 1, 12}, 3), equalTo(24));
12+
}
13+
14+
@Test
15+
void findFinalValue2() {
16+
assertThat(new Solution().findFinalValue(new int[] {2, 7, 9}, 4), equalTo(4));
17+
}
18+
}

0 commit comments

Comments
 (0)