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.

Example 2:
Input: arr = [7,1,14,11]
Output: true
Explanation: N = 14 is the double of M = 7,that is, 14 = 2 * 7.

Example 3:
Input: arr = [3,1,7,11]
Output: false
Explanation: In this case does not exist N and M, such that N = 2 * M.

 
Constraints:

2 <= arr.length <= 500
-10^3 <= arr[i] <= 10^3

Source: leetcode | Difficulty: EASY
Tags: Data structures

Sorting

Amortized analysis

Starter code: class Solution:
    def checkIfExist(self, arr: List[int]) -> bool:
        

Solutions:
class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		found = {}
		for num in arr:
			if num * 2 in found:
				return True
			if num % 2 == 0 and num / 2 in found:
				return True
			found[num] = True
		return False


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		lookup = dict()
		for val in arr:
			if val * 2 in lookup:
				return True
			elif val / 2 in lookup:
				return True
			else:
				lookup[val] = 1
		return False


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		arr2 = list([x * 2 for x in arr])
		zeros = arr.count(0)
		if zeros >= 2:
			return True
		print(arr2)
		for a in arr2:
			if a in arr:
				if a == 0 and zeros == 1:
					continue
				return True
		return False


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		tracker = set()
		for n in arr:
			if n % 2 == 0 and n / 2 in tracker or n * 2 in tracker:
				return True
			tracker.add(n)
		return False


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		for i in range(0, len(arr)):
			for j in range(i + 1, len(arr)):
				if arr[i] == 2 * arr[j] or arr[j] == 2 * arr[i]:
					return True
		return False


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		arr.sort()
		for k in range(len(arr) - 1):
			if 2 * arr[k] in arr[k + 1:] or 2 * arr[k] in arr[:k]:
				return True
		return False


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		i = 0
		i = len(arr)
		for j in range(0, i):
			for k in range(0, i):
				if arr[j] > 0 and arr[k] > 0 and (arr[j] > arr[k]):
					if arr[j] == arr[k] * 2:
						return True
				elif arr[j] < 0 and arr[k] < 0 and (arr[j] < arr[k]):
					if arr[k] * 2 == arr[j]:
						return True
		if arr[0] == 0 and arr[1] == 0:
			return True
		else:
			return False


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		arr.sort()
		for i in range(len(arr)):
			for j in range(i + 1, len(arr)):
				if arr[i] != 0 and arr[j] / arr[i] == 2:
					return True
				if arr[i] == 0 and arr[j] == 0:
					return True
				if arr[i] < 0 and arr[j] < 0 and (arr[i] / arr[j] == 2):
					return True


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		seen = set()
		for e in arr:
			if 2 * e in seen or (e % 2 == 0 and e // 2 in seen):
				return True
			seen.add(e)
		return False


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		for num in arr:
			if num == 0:
				if arr.count(0) > 1:
					return True
			elif arr.count(num * 2) != 0:
				return True
		return False


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		h_table = set()
		for num in arr:
			if num * 2 in h_table or num / 2 in h_table:
				return True
			h_table.add(num)
		return False


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		doubledList = []
		l = len(arr)
		for num in arr:
			doubledList.append(num * 2)
		for i in range(l):
			if doubledList[i] in arr and arr.index(doubledList[i]) != i:
				return True
		return False


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		cache = set()
		for (i, value) in enumerate(arr):
			if 2 * value in cache or (value % 2 == 0 and value / 2 in cache):
				return True
			cache.add(value)
		return False


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		if len(arr) < 2:
			return False
		for n in range(len(arr)):
			m = 2 * arr[n]
			for k in range(len(arr)):
				if arr[k] == m and k != n:
					return True
		return False


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		if arr[0] == 0 and arr[1] == 0:
			return True
		dictionary = {}
		for number in arr:
			dictionary[2 * number] = True
		for number in arr:
			if number in dictionary and number != 0:
				return True
		return False


class Solution:

	def checkIfExist(self, arr: List[int]) -> bool:
		for i in range(len(arr)):
			for j in range(len(arr)):
				if i != j and (arr[i] * 2 == arr[j] or arr[i] == arr[j] * 2):
					print(i)
					print(j)
					return True
		return False