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 original is found in nums, multiply it by two (i.e., set original = 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
Keep Multiplying Found Values by Twonums = [5,3,6,1,12], original = 3536112Found values highlighted361224FoundFoundFoundNot Found×2×2×2Doubling Process: 3 → 6 → 12 → 24Final Result: 24Stop when doubled value is not found in array
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
Asked in
Google 15 Amazon 12 Microsoft 8
25.0K Views
Medium Frequency
~15 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