Keep Multiplying Found Values by Two - 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.
You then do the following steps:
- If
originalis found innums, multiply it by two (i.e., setoriginal = 2 * original). - Otherwise, stop the process.
- Repeat this process with the new number as long as you keep finding the number.
Return the final value of original.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [5,3,6,1,12], original = 3
›
Output:
24
💡 Note:
3 is found → double to 6. 6 is found → double to 12. 12 is found → double to 24. 24 is not found → return 24
Example 2 — Single Step
$
Input:
nums = [2,7,9], original = 4
›
Output:
4
💡 Note:
4 is not found in the array, so return the original value 4
Example 3 — Multiple Doublings
$
Input:
nums = [1,2,4,8], original = 1
›
Output:
16
💡 Note:
1 → 2 → 4 → 8 → 16. Since 16 is not in array, return 16
Constraints
- 1 ≤ nums.length ≤ 1000
- 1 ≤ nums[i], original ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [5,3,6,1,12] and original value 3
2
Process
Keep finding and doubling: 3→6→12→24
3
Output
Return 24 when not found in array
Key Takeaway
🎯 Key Insight: Use hash set to avoid repeated array searches for O(1) lookups
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code