File: keep-multiplying-found-values-by-two/solution.py

Date: 2026-06-06

Time: 17:10

Purpose

This file solves LeetCode 2154: Keep Multiplying Found Values by Two. It owns a single responsibility: given an array of integers and a starting value, repeatedly double the value as long as it exists in the array, then return the final result.

Key Components

findfinalvalue(nums, original) -> int

The sole public function. Contract:

Patterns

Set-based lookup optimization. The list is converted to a set on line 12 before the search loop. This is the standard idiom in this repo for turning repeated O(n) membership checks into O(1) amortized lookups. Without it, the while loop would scan the full list on every iteration.

In-place accumulation. Rather than introducing a new variable, the function mutates the original parameter directly (original *= 2). This is idiomatic Python for simple accumulator patterns where the parameter name still reads clearly.

Dependencies

Imports: None — pure stdlib, no external or internal dependencies.

Imported by: The testsolution.py in the same directory imports findfinal_value. The massive "Imported By" list in the prompt is misleading — those are *other* problems' test files, likely an artifact of the analysis tool picking up a shared test harness or runner, not direct imports of this function.

Flow

1. Build num_set from nums — O(n) time, O(n) space.

2. Enter a while loop: if original is in num_set, double it.

3. The loop terminates when original is no longer in the set.

4. Return the final value.

The key insight is that original only ever increases (doubles), and the set is finite, so the loop always terminates. In the worst case, original doubles at most O(log(max(nums))) times before exceeding every element in the set.

Invariants

Error Handling

None. The function assumes valid inputs per LeetCode constraints (non-empty list, positive integers). No validation, no exceptions raised.

Topics to Explore

Beliefs