Given an array A of N integers. The task is to find if there are two pairs (a, b) and (c, d) such that a+b = c+d.
Example 1:
Input:N=7 A[] = {3, 4, 7, 1, 2, 9, 8} Output: 1Explanation:(3, 7) and (9, 1) are the pairs whosesum are equal.  
Example 2:
Input:N=7 A[] = {65, 30, 7, 90, 1, 9, 8}Output: 0
 
Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function findPairs() that takes array a and nas parametersand return 1 if pair exists else 0.
Expected Time Complexity:O(N^{2}).
Expected Auxiliary Space:O(N^{2}).
Constraints:
1 ≤ N ≤ 10^{3}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

    def findPairs(self, arr, n): 

        #code here.  



Solutions:
class Solution:

	def findPairs(self, arr, n):
		l = []
		for i in range(len(arr)):
			for j in range(i + 1, len(arr)):
				sum = arr[i] + arr[j]
				if sum in l:
					return 1
				else:
					l.append(sum)
		else:
			return 0


class Solution:

	def findPairs(self, arr, n):
		s = set()
		sum = 0
		for i in range(n):
			for j in range(i + 1, n):
				sum = arr[i] + arr[j]
				if sum in s:
					return 1
				else:
					s.add(sum)
		return 0


class Solution:

	def findPairs(self, arr, n):
		if n > 1:
			ans = 0
			arr.sort()
			mp = arr[-1] + arr[-2]
			z = [0] * (mp + 1)
			for i in range(0, n - 1):
				for j in range(i + 1, n):
					som = arr[i] + arr[j]
					z[som] = z[som] + 1
					if z[som] > 1:
						return 1
			return 0
		else:
			return 0


class Solution:

	def findPairs(self, arr, n):
		mem = {}
		for i in range(n):
			for j in range(i + 1, n):
				if arr[i] + arr[j] in mem.keys():
					return 1
				else:
					mem[arr[i] + arr[j]] = 1
		return 0


class Solution:

	def findPairs(self, arr, n):
		v = set()
		for i in range(n):
			for j in range(i + 1, n):
				if arr[i] + arr[j] in v:
					return 1
				v.add(arr[i] + arr[j])
		return 0


class Solution:

	def findPairs(self, arr, n):
		dict1 = dict()
		number = 0
		for i in range(n - 1):
			for j in range(i + 1, n):
				if arr[i] + arr[j] not in dict1:
					dict1[arr[i] + arr[j]] = 1
				else:
					return 1
		return 0


class Solution:

	def findPairs(self, arr, n):
		sum_map = {}
		for i in range(n):
			for j in range(i + 1, n):
				pair_sum = arr[i] + arr[j]
				if pair_sum in sum_map:
					return 1
				else:
					sum_map[pair_sum] = (arr[i], arr[j])
		return 0


class Solution:

	def findPairs(self, arr, n):
		ans = []
		for i in range(0, n - 1):
			for j in range(i + 1, n):
				if arr[i] + arr[j] in ans:
					return 1
				else:
					ans.append(arr[i] + arr[j])
		return 0


class Solution:

	def findPairs(self, arr, n):
		map = {}
		for i in range(n):
			for j in range(i + 1, n):
				sum = arr[i] + arr[j]
				if sum in map:
					return 1
				else:
					map[sum] = (arr[i], arr[j])
		return 0


class Solution:

	def findPairs(self, arr, n):
		su = set()
		count = -1
		for i in range(len(arr)):
			for j in range(i + 1, len(arr)):
				if arr[i] + arr[j] in su:
					count += 1
					break
				else:
					su.add(arr[i] + arr[j])
		if count == -1:
			return 0
		else:
			return 1


class Solution:

	def findPairs(self, arr, n):
		d = {}
		for i in range(len(arr) - 1):
			for j in range(i + 1, len(arr)):
				s = arr[i] + arr[j]
				if s in d:
					return 1
				else:
					d[s] = (arr[i], arr[j])
		return 0


class Solution:

	def findPairs(self, arr, n):
		if n < 4:
			return 0
		l = []
		dic = {}
		for i in range(n):
			for j in range(i + 1, n):
				s = arr[i] + arr[j]
				if s not in dic:
					dic[s] = 1
				else:
					dic[s] += 1
				if dic[s] == 2:
					return 1
		return 0


class Solution:

	def findPairs(self, arr, n):
		nums = set()
		count = 0
		for i in range(n):
			for j in range(i + 1, n):
				if arr[i] + arr[j] in nums:
					return 1
				else:
					nums.add(arr[i] + arr[j])
		return 0


class Solution:

	def findPairs(self, arr, n):
		freq = {}
		for i in range(n - 1):
			for j in range(i + 1, n):
				summ = arr[i] + arr[j]
				if summ in freq:
					return 1
				else:
					freq[summ] = 1
		return 0


class Solution:

	def findPairs(self, arr, n):
		lst = []
		cnt = 0
		s = []
		for i in range(n - 1):
			for j in range(i + 1, n):
				sm = arr[i] + arr[j]
				if sm in s:
					return 1
				else:
					s.append(sm)
		return 0


class Solution:

	def approach1(self, arr, n):
		arr.sort()
		print(arr)
		count = 0
		for i in range(n):
			for j in range(i + 1, n):
				for k in range(j + 1, n):
					for l in range(k + 1, n):
						sum1 = arr[i] + arr[j]
						sum2 = arr[k] + arr[l]
						if sum1 == sum2:
							return 1
		return 0

	def approach2(self, arr, n):
		d = {}
		for i in range(len(arr) - 1):
			for j in range(i + 1, len(arr)):
				s = arr[i] + arr[j]
				if s in d.keys():
					return 1
				else:
					d[s] = [arr[i], arr[j]]
		return 0

	def findPairs(self, arr, n):
		return self.approach2(arr, n)


class Solution:

	def findPairs(self, arr, n):
		arr.sort()
		di = dict()
		for i in range(n):
			for j in range(i + 1, n):
				add = arr[i] + arr[j]
				if add in di:
					return 1
				else:
					di[add] = [arr[i], arr[j]]
		return 0


class Solution:

	def findPairs(self, arr, n):
		_ = {}
		for i in range(n):
			for j in range(i + 1, n):
				if arr[i] + arr[j] in _:
					return 1
				else:
					_[arr[i] + arr[j]] = True
		return 0


from collections import defaultdict

class Solution:

	def findPairs(self, arr, n):
		sum_dict = defaultdict()
		for i in range(n):
			for j in range(i + 1, n):
				curr_sum = arr[i] + arr[j]
				if curr_sum in sum_dict:
					return 1
				else:
					sum_dict[curr_sum] = 1
		return 0


class Solution:

	def findPairs(self, arr, n):
		dict = {}
		for i in range(n):
			for j in range(i + 1, n):
				if dict.get(arr[i] + arr[j], 0) in dict:
					return 1
				else:
					dict[arr[i] + arr[j]] = arr[i] + arr[j]
		return 0


class Solution:

	def findPairs(self, a, n):
		mySet = set()
		for i in range(0, len(a)):
			for j in range(i + 1, len(a)):
				if a[i] + a[j] in mySet:
					return 1
				else:
					mySet.add(a[i] + a[j])
		return 0


class Solution:

	def findPairs(self, arr, n):
		m = set()
		for i in range(0, len(a)):
			for j in range(i + 1, len(a)):
				s = a[i] + a[j]
				if s in m:
					return 1
				else:
					m.add(s)
		return 0


class Solution:

	def findPairs(self, arr, n):
		myset = set()
		for i in range(len(arr)):
			for j in range(i + 1, len(arr)):
				if arr[i] + arr[j] not in myset:
					myset.add(arr[i] + arr[j])
				else:
					return 1
		return 0


class Solution:

	def findPairs(self, arr, n):
		mat = set()
		for i in range(n):
			for j in range(i, n):
				if i != j and i < j:
					if arr[i] + arr[j] not in mat:
						mat.add(arr[i] + arr[j])
					else:
						return 1
		return 0


class Solution:

	def findPairs(self, arr, n):
		dic = {}
		for i in range(n - 1):
			for j in range(i + 1, n):
				if arr[i] + arr[j] not in dic:
					dic[arr[i] + arr[j]] = 1
				else:
					dic[arr[i] + arr[j]] += 1
					return 1
		return 0