2154. Keep Multiplying Found Values by Two
EasyView on LeetCode
Problem Overview
Keep Multiplying Found Values by Two (Easy) asks you to solve a structured algorithmic task. This is a common Array / Hash Table pattern in coding interviews. Store nums in a HashSet; repeatedly double original while it exists in the set.
A full step-by-step explanation is being added. See the study guide for pattern-based practice.
Approach
Store nums in a HashSet; repeatedly double original while it exists in the set.
Related patterns: Array, Hash Table, Simulation
2154.cs
C#
// Approach: Store nums in a HashSet; repeatedly double original while it exists in the set.
// Time: O(n + log(max)) Space: O(n)
public class Solution
{
public int FindFinalValue(int[] nums, int original)
{
// Create a HashSet to store all unique values from the array for O(1) lookup
HashSet<int> numSet = new HashSet<int>(nums);
// Keep doubling the original value while it exists in the set
while (numSet.Contains(original))
// Double the original value using left shift operation (equivalent to multiplying by 2)
original <<= 1;
// Return the final value that is not present in the set
return original;
}
}Was this solution helpful?
Related Problems
- 4. Median of Two Sorted Arrays(Hard)
- 11. Container With Most Water(Medium)
- 15. 3Sum(Medium)
- 16. 3Sum Closest(Medium)
- 26. Remove Duplicates from Sorted Array(Easy)
- 27. Remove Element(Easy)