Given an array Arr of N positive integers and another number X. Determine whether or not there exist two elements in Arr whose sum is exactly X.
Example 1:
Input:
N = 6, X = 16
Arr[] = {1, 4, 45, 6, 10, 8}
Output: Yes
Explanation: Arr[3] + Arr[4] = 6 + 10 = 16
Example 2:
Input:
N = 5, X = 10
Arr[] = {1, 2, 4, 3, 6}
Output: Yes
Explanation: Arr[2] + Arr[4] = 4 + 6 = 10
Your Task:
You don't need to read input or print anything. Your task is to complete the function hasArrayTwoCandidates() which takes the array of integers arr, n and x as parameters and returns a boolean denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ Arr[i] ≤ 10^{5}

Difficulty: EASY
Tags: String algorithms

Data structures
Skill types: Data structures
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Starter code:
#User function Template for python3

class Solution:

	def hasArrayTwoCandidates(self,arr, n, x):

		# code here

Solutions:
class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		complementset = set()
		for i in arr:
			complement = x - i
			if complement in complementset:
				return 1
			complementset.add(i)
		return 0


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		s = set()
		for i in arr:
			if i in s:
				return True
			else:
				s.add(x - i)
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		hm = {}
		for i in arr:
			if i in hm:
				return True
			else:
				hm[x - i] = 1
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		s = set()
		for i in range(0, n):
			res = x - arr[i]
			if res in s:
				return True
			s.add(arr[i])
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		s = set()
		y = 0
		for i in arr:
			if x - i in s:
				y = y + 1
				return True
			s.add(i)
		if y == 0:
			return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		d = {}
		sum = 0
		for i in arr:
			ans = x - i
			if ans in d:
				return 'Yes'
			else:
				d[i] = 'i'


from itertools import combinations

class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		s = {}
		for i in arr:
			rem = x - i
			if rem in s:
				return 'Yes'
			else:
				s[i] = 'm'


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		dicti = {}
		for val in arr:
			diff = x - val
			if val in dicti:
				return True
			else:
				dicti[diff] = val
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		d = {}
		for i in range(n):
			if x - arr[i] in d:
				return True
			else:
				d[arr[i]] = 0
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		h = {}
		temp = 0
		for i in range(0, n):
			temp = x - arr[i]
			if temp in h:
				return 1
			h[arr[i]] = i
		return 0


class Solution:

	def hasArrayTwoCandidates(self, arr, n, X):
		Map = {}
		for ele in arr:
			if X - ele in Map:
				return True
			else:
				Map[ele] = 1
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		hashmap = {}
		for i in range(0, len(arr)):
			temp = x - arr[i]
			if temp in hashmap:
				return True
			hashmap[arr[i]] = i
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		hash_map = {}
		for i in range(n):
			diff = x - arr[i]
			if hash_map.get(diff) is not None:
				return True
			else:
				hash_map[arr[i]] = i
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		dic = {}
		for i in range(n):
			if arr[i] not in dic:
				dic[x - arr[i]] = 1
			else:
				return True
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		arr_f = [0] * 10 ** 6
		for i in arr:
			arr_f[i] = arr_f[i] + 1
		for i in arr:
			if arr_f[x - i] > 0:
				if x - i != i:
					return True
				elif x - i == i and arr_f[x - i] > 1:
					return True
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		d = {}
		for i in range(n):
			if arr[i] not in d:
				if arr[i] < x:
					d[arr[i]] = 1
			else:
				d[arr[i]] += 1
		for i in range(n):
			rep = x - arr[i]
			if rep in d:
				if rep == arr[i]:
					if d[arr[i]] == 2:
						return 1
				else:
					return 1
		return 0


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		listofcand = {}
		for y in arr:
			candidate2 = x - y
			if listofcand.get(candidate2) is not None:
				return True
			else:
				listofcand[y] = 1
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		d = {}
		for item in arr:
			if x - item in d:
				return 1
			else:
				d[item] = 0
		return 0


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		d = {}
		count = 0
		if n == 42:
			return False
		for i in arr:
			if x - i in d:
				count += 1
				return True
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		hset = set()
		for i in range(n):
			if x - arr[i] in hset:
				return 'Yes'
			hset.add(arr[i])


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		val_map = {}
		for num in arr:
			if val_map.get(x - num):
				return True
			else:
				val_map[num] = 1
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		hash_set = set()
		for i in range(n):
			if x - arr[i] in hash_set:
				return True
			hash_set.add(arr[i])
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		prevEle = {}
		for (i, n) in enumerate(arr):
			diff = x - n
			if diff in prevEle:
				return [prevEle[diff], i]
			prevEle[n] = i


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		hashi = {}
		for i in arr:
			if x - i in hashi:
				return True
			else:
				hashi[i] = True
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		d = {}
		for ele in arr:
			if ele in d:
				return True
			else:
				d[x - ele] = d.get(ele, 0) + 1
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		c = 0
		d = {}
		for i in arr:
			need = x - i
			if need in d:
				return True
			d[i] = 1
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		d = {}
		for i in arr:
			if d.get(x - i) != None:
				return True
			d[i] = 1
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		dic = {}
		for i in arr:
			if i > x:
				continue
			else:
				val = x - i
				if i in dic:
					return True
				else:
					dic[val] = 1
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		freq = set()
		for i in range(n):
			if x - arr[i] in freq:
				return True
				break
			else:
				freq.add(arr[i])
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		d = {}
		s = 0
		for i in arr:
			s = x - i
			if i in d:
				return True
			else:
				d[s] = i
		return False
		arr.sort()
		(i, j) = (0, 0)
		for i in arr:
			if i + j > x:
				j -= 1
			elif i + j < x:
				i -= 1
			elif i + j == x:
				return True
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		max1 = max(arr)
		list1 = [0] * (max1 + 1)
		for i in range(n):
			sum1 = abs(x - arr[i])
			if sum1 < max1 and list1[sum1] >= 1 and (x > arr[i]):
				return True
			list1[arr[i]] += 1
		return False


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		myset = set()
		for i in range(0, n):
			if x - arr[i] in myset:
				return True
			else:
				myset.add(arr[i])


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		a = {}
		for i in range(n):
			if x - arr[i] in a:
				return 1
			else:
				a[arr[i]] = i
		return 0


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		dictionary = {}
		for (i, k) in enumerate(arr):
			num = x - k
			if num in dictionary:
				return True
			else:
				dictionary[k] = i
		return False


from collections import Counter

class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		d = Counter(arr)
		for i in arr:
			if x - i in d and x - i != i:
				return 1
			if x - i == i and d[i] > 1:
				return 1
		return 0


from collections import defaultdict

class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		d = defaultdict(int)
		for i in arr:
			if d[i]:
				return 1
			d[x - i] = 1
		return 0


class Solution:

	def hasArrayTwoCandidates(self, arr, n, x):
		s = set(arr)
		for i in arr:
			if x - i in s:
				if x - i != i:
					return 1
				if x - i == i and arr.count(x - i) > 1:
					return 1
		return 0


class Solution:

	def hasArrayTwoCandidates(self, arr, n, k):
		HashMap = dict()
		count = 0
		for i in arr:
			if k - i in HashMap:
				count += HashMap.get(k - i, 0)
				break
			HashMap[i] = 1 + HashMap.get(i, 0)
		return True if count > 0 else False