Keep Multiplying Found Values by Two (LeetCode 2154): Solution and Explanation
Start from original. As long as the current value appears in nums, double it. Return the first value that is not in nums.
Intuition
The process visits a fixed chain of values: original, 2 x original, 4 x original, and so on. It stops at the first link of the chain that nums does not contain.
The only question asked over and over is "is this value in nums?". Scanning the array each time would work, but a hash set answers it in constant time.
The chain also cannot be long. Values in nums are at most 1000 and original at least doubles each round, so after about ten doublings it is guaranteed to fall outside nums.
Approach: Hash set and a doubling loop
- Put every element of
numsinto a setseen. - While
originalis inseen, setoriginal = original * 2. - Return
original.
Step-by-step example
nums = [5,3,6,1,12], original = 3
- seen = {1, 3, 5, 6, 12}.
- 3 is in seen, so original becomes 6.
- 6 is in seen, so original becomes 12.
- 12 is in seen, so original becomes 24.
- 24 is not in seen. Return 24.
Code: Python, Java, C++
1class Solution:
2 def findFinalValue(self, nums: List[int], original: int) -> int:
3 seen = set(nums)
4 while original in seen:
5 original *= 2
6 return original1class Solution {
2 public int findFinalValue(int[] nums, int original) {
3 Set<Integer> seen = new HashSet<>();
4 for (int x : nums) {
5 seen.add(x);
6 }
7 while (seen.contains(original)) {
8 original *= 2;
9 }
10 return original;
11 }
12}1class Solution {
2public:
3 int findFinalValue(vector<int>& nums, int original) {
4 unordered_set<int> seen(nums.begin(), nums.end());
5 while (seen.count(original)) {
6 original *= 2;
7 }
8 return original;
9 }
10};Time and space complexity
O(n)
Building the set is O(n). The loop runs at most O(log(max value)) times, about 11 here, with O(1) lookups.
O(n)
The set stores up to n distinct values.
Other approaches
Sort and sweep (time O(n log n), space O(1) extra)
Sort nums, then walk it once; whenever the current value equals original, double original. Because the chain is increasing, one sweep finds every link.
Scan for every lookup (time O(n log V), space O(1))
Search the array for original on every round. Correct, and only about 11 rounds happen, but each costs O(n).
Common mistakes
- Stopping after a single doubling. The check repeats until original is missing.
- Assuming the answer must be at most 1000. It can be larger, for example 1000 doubles to 2000 when 1000 is in nums.
- Building the set inside the loop, which repeats O(n) work every round.
Frequently asked questions
Can the loop run forever?
No. original is at least 1 and doubles each time, while nums only holds values up to 1000. After at most about 11 rounds it exceeds every element and the loop ends.
Do duplicates in nums matter?
No. Only membership matters, so the set simply keeps one copy of each value.
Is the hash set worth it for such a short loop?
Either way passes. The set gives a clean O(n) bound and states the intent clearly: repeated membership checks.