Keep Multiplying Found Values by Two — LeetCode 2154 Python Solution

EasyArrayHash TableSortingSimulation
Problem
#2154
Pattern
Sorting
Reading time
2 min

The problem

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.

Example

Input
nums = [5,3,6,1,12], original = 3
Output
24
Explanation
- 3 is found in nums. 3 is multiplied by 2 to obtain 6.

Python solution

Python
class Solution:
    def findFinalValue(self, nums: List[int], original: int) -> int:
        s = set(nums)
        while original in s:
            original <<= 1
        return original

Complexity

MeasureComplexity
TimeO(n)
SpaceO(n) auxiliary

Pattern: Sorting

Spend O(n log n) once to buy an ordering that makes the rest of the problem trivial. LeetCode 2154. Keep Multiplying Found Values by Two is filed here because LeetCode tags it Sorting, which is the vocabulary this hub collects.

The sorting guide has the Python template for the pattern and the 401 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2154. Keep Multiplying Found Values by Two?
LeetCode 2154. Keep Multiplying Found Values by Two is rated Easy on LeetCode.
What is the time complexity of LeetCode 2154. Keep Multiplying Found Values by Two?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 2154. Keep Multiplying Found Values by Two?
The Python solution on this page uses O(n) auxiliary space.
What topics does LeetCode 2154. Keep Multiplying Found Values by Two cover?
LeetCode 2154. Keep Multiplying Found Values by Two is tagged Array, Hash Table, Sorting and Simulation on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview