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:

  1. Search Phase: Look for the current value of original in the array
  2. Double Phase: If found, multiply original by 2 (i.e., original = 2 * original)
  3. Repeat Phase: Continue the process with the new doubled value
  4. 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
🏴‍☠️ The Treasure Doubling Adventure📜 Treasure Map (Hash Set)💰5💰3💰6💰1💰12🏴‍☠️ Pirate's Journey3Start6Double!12Double!24Stop!Algorithm Magic ✨1. Create treasure map (hash set)• O(1) lookups instead of O(n)2. Start with original coins3. While coins found in map:• Double the coins• Check again (O(1) time!)4. Return final coin countTime: O(n + k) | Space: O(n)
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!
Asked in
Google 23 Amazon 18 Meta 15 Microsoft 12
24.5K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen