2154. Keep Multiplying Found Values by Two
Difficulty: Easy
Topics: Array, Hash Table, Sorting, Simulation, Weekly Contest 278
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.
Example 1:
- Input: nums = [5,3,6,1,12], original = 3
- Output: 24
-
Explanation:
- 3 is found in nums. 3 is multiplied by 2 to obtain 6.
- 6 is found in nums. 6 is multiplied by 2 to obtain 12.
- 12 is found in nums. 12 is multiplied by 2 to obtain 24.
- 24 is not found in nums. Thus, 24 is returned.
Example 2:
- Input: nums = [2,7,9], original = 4
- Output: 4
-
Explanation:
- 4 is not found in nums. Thus, 4 is returned.
Constraints:
1 <= nums.length <= 10001 <= nums[i], original <= 1000
Hint:
- Repeatedly iterate through the array and check if the current value of original is in the array.
- If original is not found, stop and return its current value.
- Otherwise, multiply original by 2 and repeat the process.
- Use set data structure to check the existence faster.
Solution:
We need to repeatedly check if the current value of original exists in the given array nums. If it does, we double original and continue checking. This process stops when original is no longer found in nums.
Approach:
-
Efficient Lookup: Convert the array
numsinto a set for O(1) average time complexity lookups. -
Iterative Checking: Continuously check if the current value of
originalexists in the set. If it does, doubleoriginaland repeat the process. If not, return the current value oforiginal.
Let's implement this solution in PHP: 2154. Keep Multiplying Found Values by Two
<?php
/**
* @param Integer[] $nums
* @param Integer $original
* @return Integer
*/
function findFinalValue($nums, $original) {
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo findFinalValue([5,3,6,1,12], 3) . "\n"; // Output: 24
echo findFinalValue([2,7,9], 4) . "\n"; // Output: 4
echo findFinalValue([1,2,3], 10) . "\n"; // Output: 10
?>
Explanation:
-
Convert to Set: Using
array_flipconverts the array into a set-like structure where the values fromnumsbecome keys. This allows O(1) average time complexity for membership checks. -
Loop Until Not Found: The while loop checks if the current
originalexists in the set. If it does,originalis doubled. This continues untiloriginalis not found in the set, at which point the loop exits and the current value oforiginalis returned.
This approach efficiently checks for the existence of original and its subsequent doubled values, ensuring optimal performance even for the upper constraint limits. The use of a set ensures that each check is done in constant time, making the overall time complexity O(n) in the worst case, where n is the number of elements in nums.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)