Editorial
Solution walkthrough
Review the explanation, then compare the reference implementation.
Video explanation
Solution
Intuition
Each step asks only whether the current value appears anywhere in the array; its position and number of occurrences do not matter. A set makes repeated membership checks direct.
Approach
Build a set of all input values. While the current original value belongs to the set, double it. Return the first value that is absent. Under the positive-value constraints, doubling strictly increases the value, so it eventually exceeds all stored values and the process terminates. If the initial value is absent, it is returned unchanged, regardless of whether some later doubled value would be present.
class Solution:
def findFinalValue(self, nums: List[int], original: int) -> int:
s = set(nums)
while original in s:
original *= 2
return originalComplexity analysis
Let n = nums.length and q be the number of successful doublings. Time: O(n + q) expected. Auxiliary space: O(n) for the membership set.
