Keep Multiplying Found Values by Two
The problem requires us to continuously search for a number in the array and double it each time we find it. We stop when we can’t find the current number in the array and return this number.
Here is a Python solution:
|
|
The solution begins by converting the input list nums
to a set for faster lookup. It then enters a loop where it doubles original
while original
exists in the set. This process stops when original
no longer exists in the set. The final value of original
is then returned.
This has a time complexity of O(n), where n is the length of the input array nums
, as each element is visited once when converting the list to a set. The space complexity is also O(n) due to the extra space required for the set.