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.
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≤500
−103≤arr[i]≤103
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