Keep Multiplying Found Values by Two - Problem
You are given an array of integers nums and an integer original representing your starting value. Your task is to implement a value doubling simulation that follows these rules:
- Search Phase: Look for the current value of
originalin the array - Double Phase: If found, multiply
originalby 2 (i.e.,original = 2 * original) - Repeat Phase: Continue the process with the new doubled value
- Stop Phase: If the current value is not found in the array, stop the process
Return the final value of original after the simulation completes.
Example: If nums = [5,3,6,1,12] and original = 3, we find 3 → double to 6 → find 6 → double to 12 → find 12 → double to 24 → can't find 24, so return 24.
Input & Output
example_1.py — Basic Doubling
$
Input:
nums = [5,3,6,1,12], original = 3
›
Output:
24
💡 Note:
Start with 3 → found, double to 6 → found, double to 12 → found, double to 24 → not found, return 24
example_2.py — Not Found Initially
$
Input:
nums = [2,7,9], original = 4
›
Output:
4
💡 Note:
Start with 4 → not found in array, so return 4 immediately without any doubling
example_3.py — Single Element
$
Input:
nums = [8], original = 8
›
Output:
16
💡 Note:
Start with 8 → found, double to 16 → not found, return 16
Constraints
- 1 ≤ nums.length ≤ 1000
- 1 ≤ nums[i], original ≤ 1000
- All integers in nums are distinct
- At most 10 doubling operations will occur
Visualization
Tap to expand
Understanding the Visualization
1
Map the Treasure Island
First, create a treasure map (hash set) of all chest contents for instant lookups
2
Start Your Quest
Begin with your original coin count and check if it exists in any chest
3
Magic Doubling
If found, your coins magically double and you search for the new amount
4
End of Adventure
When no chest contains your coin count, the quest ends
Key Takeaway
🎯 Key Insight: Converting the array to a hash set enables instant O(1) treasure chest lookups, making the doubling adventure efficient even with many treasures!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code