Check If N and Its Double Exist - Problem
Given an array arr of integers, check if there exist two indices i and j such that:
i != j0 <= i, j < arr.lengtharr[i] == 2 * arr[j]
Return true if such indices exist, otherwise return false.
Input & Output
Example 1 — Basic Double Found
$
Input:
arr = [10,2,5,3]
›
Output:
true
💡 Note:
We can find 10 and 5 where 10 == 2 * 5, so there exists i=0 and j=2 such that arr[i] == 2 * arr[j]
Example 2 — No Double Exists
$
Input:
arr = [3,1,7,11]
›
Output:
false
💡 Note:
No element in the array is exactly double another element: 3≠2×1, 3≠2×7, 3≠2×11, and no other pairs work
Example 3 — Zero Edge Case
$
Input:
arr = [0,0]
›
Output:
true
💡 Note:
We have two zeros, and 0 == 2 * 0, so arr[0] == 2 * arr[1] with different indices i=0, j=1
Constraints
- 2 ≤ arr.length ≤ 500
- -103 ≤ arr[i] ≤ 103
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given array [10,2,5,3] - need to find double pairs
2
Check Relationships
For each element, check if 2×element or element÷2 exists
3
Found Match
10 and 5 satisfy: 10 == 2×5, return true
Key Takeaway
🎯 Key Insight: Use a hash set to check if 2×current or current÷2 already exists in one pass
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code