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 != j
  • 0 <= i, j < arr.length
  • arr[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
Check If N and Its Double ExistFind if any two elements have relationship: arr[i] == 2 * arr[j]10253index 0index 1index 2index 3Double Relationship Found!10 == 2 × 5 ✓Output: trueFound indices i=0, j=2 where arr[i] == 2 * arr[j]
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
Asked in
Google 12 Amazon 8 Microsoft 6 Apple 4
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen