Check If N and Its Double Exist (LeetCode 1346): Solution and Explanation
Given an integer array arr, decide whether two different positions hold values where one is exactly twice the other. Negative numbers and zero are allowed, which is where most bugs hide.
Intuition
Comparing every pair of positions answers the question in O(n²), which is fine for 500 elements but wasteful. The faster idea is the same one behind Two Sum: when you reach the second element of a matching pair, the first is already behind you, so remember everything you have passed in a hash set.
The twist is that the pair works in two directions. The current value x might be the smaller one, so its partner 2x could be behind you. Or x might be the larger one, so its partner x / 2 could be behind you. You do not know which element of the pair arrives first, so ask for both.
x / 2 only makes sense when x is even. For odd x, integer division would round, and 7 would wrongly look for 3. Guard the half lookup with x % 2 == 0.
Zero is the special case: 0 is twice 0. The check happens before x is added to the set, so a single 0 never finds itself. A second 0 does find the first one, which is exactly the rule that i and j must differ.
Approach: One pass with a hash set
- Create an empty set
seen. - For each value
xinarr, check whether2 * xis inseen. - If
xis even, also check whetherx / 2is inseen. - If either lookup succeeds, return true.
- Otherwise add
xtoseenand continue. - Return false after the loop.
Step-by-step example
arr = [10,2,5,3]
- x = 10: look for 20 and 5. The set is empty, so add 10.
- x = 2: look for 4 and 1. Neither is in {10}, so add 2.
- x = 5: look for 10, which is in the set. 10 at index 0 is twice 5 at index 2, so return true.
Code: Python, Java, C++
1class Solution:
2 def checkIfExist(self, arr: List[int]) -> bool:
3 seen = set()
4 for x in arr:
5 if 2 * x in seen or (x % 2 == 0 and x // 2 in seen):
6 return True
7 seen.add(x)
8 return False1class Solution {
2 public boolean checkIfExist(int[] arr) {
3 Set<Integer> seen = new HashSet<>();
4 for (int x : arr) {
5 if (seen.contains(2 * x) || (x % 2 == 0 && seen.contains(x / 2))) {
6 return true;
7 }
8 seen.add(x);
9 }
10 return false;
11 }
12}1class Solution {
2public:
3 bool checkIfExist(vector<int>& arr) {
4 unordered_set<int> seen;
5 for (int x : arr) {
6 if (seen.count(2 * x) || (x % 2 == 0 && seen.count(x / 2))) {
7 return true;
8 }
9 seen.insert(x);
10 }
11 return false;
12 }
13};Time and space complexity
O(n)
Each element does at most two average O(1) set lookups and one insertion.
O(n)
In the worst case every value goes into the set before the loop ends.
Other approaches
Check every pair (time O(n²), space O(1))
Two nested loops over i != j testing arr[i] == 2 * arr[j]. With n at most 500 this is only 250,000 checks, so it passes, but it does not scale.
Sort and binary search (time O(n log n), space O(1) to O(n) depending on the sort)
Sort the array, then for each element binary search for twice its value at a different index. It uses no hash set, but zero still needs care because 0 can match itself.
Common mistakes
- Only checking 2x. In [5, 10] the pair is found when 10 is reached, and its partner 5 is its half, so a 2x-only check misses it.
- Looking up x / 2 for odd x. Integer division turns 7 / 2 into 3, which can create false matches.
- Adding x to the set before checking it, which lets a single 0 match itself.
- Assuming the order of the pair. The scan sees each pair only once, from whichever element comes second, so both lookups are needed every time.
Frequently asked questions
Why do we need to check both 2x and x / 2?
The scan visits each pair in one order only. If the half comes first, the double finds it through x / 2. If the double comes first, the half finds it through 2x.
How does the solution handle zero?
0 is twice 0, so it matches only another 0 at a different index. Because each x is checked before it is added, the first 0 finds nothing and the second 0 finds the first.
Do negative numbers need special handling?
No. -6 is twice -3 and the same lookups work, as long as the half lookup is only done for even values.
What is the time complexity of Check If N and Its Double Exist?
O(n) average time and O(n) space with the hash set approach.