Check If N and Its Double Exist

Given an array arr of integers, check if there exist two indices i and j such that i != j, 0 <= i, j < arr.length, and arr[i] == 2 * arr[j]. Return true if such a pair exists, otherwise false.

30:00

Check If N and Its Double Exist
easy
Free written solution
Topics
Companies

Given an array arr of integers, check if there exist two indices i and j such that i != j, 0 <= i, j < arr.length, and arr[i] == 2 * arr[j]. Return true if such a pair exists, otherwise false.

Example 1:
Input: {"arr":[10,2,5,3]}
Output: true
Constraints:
  • 2≤arr.length≤5002 \leq \text{arr.length} \leq 500

  • −103≤arr[i]≤103-10^3 \leq \text{arr}[i] \leq 10^3

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"arr":[10,2,5,3]}

Keep a set of the values already passed. For each new x, its partner is either 2x or x / 2, and if that partner came earlier it is already in the set.

arr

10

0

2

1

5

2

3

3

seen set, laid out in sorted order (0 values)

(empty)

dashed chips mark where a missing 2x or x/2 would have to be

Variables
VariableValue
n4
seen{}
DepthFunction Call
Stack empty
0/6