Problem Link: https://leetcode.com/problems/check-i...
Solution Description:
This problem checks whether an array contains two elements, one of which is twice the other. To solve this, we use a HashSet to efficiently track numbers we've encountered as we iterate through the array.
Key Steps:
Initialize a HashSet: This helps track numbers we've seen so far.
Iterate Through the Array: For each number:
Check if either 2 * num (double of the current number) or num / 2 (half of the current number) exists in the set.
If either condition is true, return true.
Add the Number to the Set: After performing the checks, add the current number to the set to account for future elements.
Return False: If no such pair exists by the end of the loop, return false.
Time Complexity:
O(n): Each number is processed once, and operations with the HashSet (add and check) are O(1) on average.
Space Complexity:
O(n): The space required for the HashSet to store up to n elements.
This approach efficiently solves the problem without requiring nested loops, making it optimal for larger input sizes.
📅 Daily Solutions:
Stay tuned for concise explanations and efficient Java implementations for LeetCode’s daily challenges!
👥 Join the Discussion:
Have ideas to optimize or improve this approach? Let’s discuss in the comments below!