1346. Check If N and Its Double Exist - Detailed Explanation
Problem Statement
You are given an array of integers, arr. The task is to determine whether there exists two distinct indices i and j such that:
[ \text{arr}[i] = 2 \times \text{arr}[j] ]
Return true if such a pair exists; otherwise, return false.
Example 1
- Input: [10, 2, 5, 3]
- Output: true
- Explanation:
 For this array, 10 is exactly double 5. Therefore, the condition is satisfied.
Example 2
- Input: [7, 1, 14, 11]
- Output: true
- Explanation:
 In this case, 14 is double 7, so the answer is true.
Example 3
- Input: [3, 1, 7, 11]
- Output: false
- Explanation:
 No element in this array is double another element, so the answer is false.
Constraints
- (2 \leq \text{arr.length} \leq 500)
- (-10^3 \leq \text{arr}[i] \leq 10^3)
Note:
Special attention is required for the value 0 because if there are at least two zeros in the array, then 0 is considered double of 0.
Hints to Guide Your Approach
- 
Use a Hash Set or Frequency Map: 
 Since you need to quickly check whether a value (or its half) exists, a hash set (or frequency map) is an excellent choice.- For each number n, check if 2 * n is already seen.
- Also, if n is even, check if n / 2 is in the set.
 This way, you cover both cases where n might be the double or the half of another number.
 
- 
Handling Zero: 
 When n is 0, since (0 = 2 \times 0), ensure that the algorithm correctly identifies that at least two 0s must be present.
Approaches to Solve the Problem
Approach 1: Hash Set / Frequency Map Method (Optimal)
Explanation
- 
Idea: 
 Traverse through the array while maintaining a hash set (or frequency map) of numbers encountered so far. For every element n, check whether:- 2 * n exists in the set, or
- If n is even, whether n / 2 exists in the set.
 
- 
Steps: - Initialize an empty set.
- Iterate over each number n in the array.
- Check if 2 * n or (if n is even) n / 2 is in the set.
- If found, return true immediately.
- Otherwise, add n to the set.
- If no valid pair is found after processing all elements, return false.
 
- 
Handling Special Case (0): 
 When processing 0, the check ensures that if another 0 is found later (or if 0 is already in the set), the function returns true.
Complexity Analysis
- Time Complexity: (O(n)), since each element is processed once.
- Space Complexity: (O(n)), to store the elements in the hash set.
Approach 2: Sorting and Binary Search
Explanation
- 
Idea: 
 First, sort the array. Then, for each element, perform a binary search for its double.
- 
Steps: - Sort the array.
- For each element n in the sorted array, perform a binary search to look for 2 * n.
- Return true if found (ensuring that the indices are different).
- If no such pair exists, return false.
 
- 
Note: 
 This approach runs in (O(n \log n)) due to the sort and binary search steps and is slightly less efficient than the hash set approach for this problem.
Complexity Analysis
- Time Complexity: (O(n \log n))
- Space Complexity: (O(1)) or (O(n)) if sorting requires additional space.
4. Code Implementations
Python Code
Below is the Python implementation using the hash set approach:
Java Code
Step-by-Step Walkthrough & Visual Examples
Walkthrough of the Hash Set Approach
Consider the array [10, 2, 5, 3]:
- 
Initialization: - Start with an empty set: seen = {}.
 
- Start with an empty set: 
- 
Iteration: - First Element:
- num = 10
- Check if (2 \times 10 = 20) is in seen→ No.
- 10 is even; check if (10 / 2 = 5) is in seen→ No.
- Add 10 to seen:seen = {10}.
 
- Second Element:
- num = 2
- Check if (2 \times 2 = 4) is in seen→ No.
- 2 is even; check if (2 / 2 = 1) is in seen→ No.
- Add 2 to seen:seen = {10, 2}.
 
- Third Element:
- num = 5
- Check if (2 \times 5 = 10) is in seen→ Yes (10 is inseen).
- Since a valid pair is found (5 and 10), the function returns true.
 
 
- First Element:
- 
Conclusion: 
 The method returns true because 10 (which is already in the set) is double 5.
Additional Sections
Common Mistakes
- Ignoring the Case When n is 0:
 Since (0 = 2 \times 0), failing to correctly handle duplicate zeros may result in an incorrect answer.
- Not Checking Both Directions:
 Ensure you check both whether the current number’s double exists and, if applicable, whether its half exists.
- Using Inefficient Brute Force Methods:
 A double loop that checks every pair leads to an (O(n^2)) time complexity, which is unnecessary for the problem size.
Edge Cases
- Single Element Array:
 The array must have at least two elements for a valid pair. An array with a single element should return false.
- Multiple Zeros:
 An array with two or more zeros should return true.
- Negative Numbers:
 The same logic applies for negative numbers since the doubling relationship holds regardless of sign.
Alternative Variations & Related Problems
- Variations:
- Check if there exists a pair such that one number is a multiple (other than double) of the other.
- Modify the problem to look for a pair with a fixed multiplication factor.
 
- Related Problems for Further Practice:
- "Two Sum" – finding a pair of numbers that add up to a target.
- "Find Pair With Given Sum" – similar pairing problems.
- "Contains Duplicate" – using hash sets to detect repeated elements.
 
Complexity Recap
- Hash Set Approach:
- Time Complexity: (O(n)) since each element is processed once.
- Space Complexity: (O(n)) for storing the elements in the hash set.
 
- Sorting and Binary Search Approach:
- Time Complexity: (O(n \log n)) due to sorting and binary search steps.
- Space Complexity: (O(1)) or (O(n)) depending on the sorting algorithm used.
 
GET YOUR FREE
Coding Questions Catalog
$197

$78
$78