Given an array arr of integers, check if there exists two integers N and M such that N is the double of M ( i.e. N = 2 * M). More formally check if there exists two indices i and j such that :
i != j
0 <= i, j < arr.length
arr[i] == 2 * arr[j]
Example 1: Input: arr = [10,2,5,3] Output: true Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5.
- code
class Solution:
def checkIfExist(self, arr: List[int]) -> bool:
potential = []
for v in arr:
if v not in potential:
potential.append(v * 2)
if v % 2 == 0:
potential.append(v // 2)
else:
return True
return False
- code
class Solution:
def checkIfExist(self, A: List[int]) -> bool:
if A.count(0) > 1: return True
S = set(A) - {0}
for a in A:
if 2*a in S: return True
return False