Skip to content

Commit 0814caa

Browse files
committed
2154. Keep Multiplying Found Values by Two
1 parent 9292d89 commit 0814caa

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.rkabanov.leetcode.p2154;
2+
3+
import java.util.HashSet;
4+
5+
/**
6+
* <a href="https://leetcode.com/problems/keep-multiplying-found-values-by-two/description/">2154. Keep Multiplying Found Values by Two</a>
7+
*/
8+
public class Solution {
9+
public int findFinalValue(int[] nums, int original) {
10+
// unsorted (1,1000) numbers [5, 3, 6, 1, 12]
11+
HashSet<Integer> set = new HashSet<>(nums.length);
12+
for (int num : nums) {
13+
set.add(num);
14+
}
15+
16+
// given 3, search 3, 6, 12, 24, 48, ...
17+
int candidate = original;
18+
19+
while (set.contains(candidate)) {
20+
candidate *= 2;
21+
}
22+
23+
return candidate;
24+
}
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.rkabanov.leetcode.p2154;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class SolutionTest {
8+
@Test
9+
public void testExample1() {
10+
var solution = new Solution();
11+
int result = solution.findFinalValue(new int[]{5, 3, 6, 1, 12}, 3);
12+
assertEquals(24, result);
13+
}
14+
15+
@Test
16+
public void testExample2() {
17+
var solution = new Solution();
18+
int result = solution.findFinalValue(new int[]{2, 7, 9}, 4);
19+
assertEquals(4, result);
20+
}
21+
}

0 commit comments

Comments
 (0)