Check If N and Its Double Exist
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 != j0 <= i, j < arr.lengtharr[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.count all numbers and store in counter,
if there are multiple zero return True
check each number's double in counter, if it is at least one, return True,
return False if above conditions fail
class Solution:
def checkIfExist(self, arr: List[int]) -> bool:
count = collections.Counter(arr)
for i in arr:
if count[i*2] and i != 0 or count[0] > 1 :
return True
return FalseLast updated
Was this helpful?