[LeetCode] Keep Multiplying Found Values by Two

2154. Keep Multiplying Found Values by Two

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.

You then do the following steps:

  1. If original is found in nums, multiply it by two (i.e., set original = 2 * original).
  2. Otherwise, stop the process.
  3. Repeat this process with the new number as long as you keep finding the number.

Return the final value of original.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int findFinalValue(vector<int>& nums, int original) {
unordered_set<int> us;
for(auto& n : nums) us.insert(n);
int res = original;
while(us.count(res)) res<<=1;
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/11/19/PS/LeetCode/keep-multiplying-found-values-by-two/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.