Given an integer array and a non-negative integer k, count all distinct pairs with difference equal to k, i.e., A[ i ] - A[ j ] = k
 
Example 1:
Input: array = {1, 5, 4, 1, 2}, k = 0
Output: 1
Explanation: There is only one pair (1, 1)
whose difference equal to 0.
Example 2;
Input: array = {1, 5, 3}, k = 2
Output: 2
Explanation: There are two pairs (5, 3) and 
(1, 3) whose difference equal to 2.
 
Your Task:
You don't need to read or print anything. Your task is to complete the function TotalPairs() which takes array and k as input parameter and returns count of all distinct pairs.
 
Expected Time Complexity: O(Max(A_{i}))
Expected Space Complexity: O(Max(A_{i}))
 
Constraints:
0 <= length of array, k <= 100000
1 <= elements of array <= 100000

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3



class Solution:

	def TotalPairs(self, nums, k):

		# Code here

Solutions:
class Solution:

	def TotalPairs(self, nums, k):
		total = 0
		d = set()
		r = []
		for n in nums:
			if n - k in d:
				r.append([n - k, n])
				if k == 0:
					d.add(n)
					continue
			if n + k in d:
				r.append([n + k, n])
			d.add(n)
		di = dict()
		for x in r:
			sum = x[0] + x[1]
			if sum not in di:
				total += 1
				di[sum] = 1
		return total


class Solution:

	def TotalPairs(self, nums, k):
		dicti = {}
		ans = set()
		for i in nums:
			if i not in dicti:
				dicti[i] = 1
		for i in nums:
			if i - k in dicti:
				ans.add((i, i - k))
		return len(ans)


class Solution:

	def TotalPairs(self, nums, k):

		def fact(num):
			factorial = 1
			X = 1
			for i in range(1, num + 1):
				if i == num - 2:
					X = factorial
				factorial = factorial * i
			return [factorial, X]
		nums.sort()
		s = 0
		e = 1
		count = 0
		while s < e and e < len(nums):
			if nums[e] - nums[s] > k:
				s += 1
				if s == e:
					e += 1
			elif nums[e] - nums[s] < k:
				e += 1
			elif nums[e] - nums[s] == k:
				if nums[s] == nums[e]:
					num = e - s + 1
					[f, x] = fact(num)
					count += f // (x * 2)
					break
				else:
					(ts, te) = (s, e)
					(count1, count2) = (0, 0)
					while s < e and e < len(nums):
						if nums[s] != nums[ts] and nums[e] != nums[te]:
							break
						if nums[s] == nums[ts]:
							s += 1
						if nums[e] == nums[te]:
							e += 1
					count += 1
		return count


class Solution:

	def TotalPairs(self, nums, k):

		def fact(n):
			f = 1
			for i in range(1, n + 1):
				f *= i
			return f
		nums.sort()
		s = 0
		e = 1
		n = len(nums)
		count = 0
		while s < e and e < n:
			if nums[e] - nums[s] < k:
				e += 1
			elif nums[e] - nums[s] > k:
				if e == s + 1:
					e += 1
					s += 1
				else:
					s += 1
			elif nums[e] - nums[s] == k:
				if nums[e] == nums[s]:
					t = e - s + 1
					count += fact(t) // (2 * fact(t - 2))
					break
				else:
					ce = 1
					cs = 1
					while e < n - 1 and nums[e] == nums[e + 1]:
						e += 1
						ce += 1
					while s < e and nums[s] == nums[s + 1]:
						s += 1
						cs += 1
					count += 1
					e += 1
					s += 1
		return count


class Solution:

	def TotalPairs(self, nums, k):
		d = set(nums)
		ans = 0
		for i in d:
			if i + k in d:
				ans += 1
		return ans


class Solution:

	def TotalPairs(self, nums, k):
		mapp = {}
		summ = 0
		for i in nums:
			if i in mapp:
				mapp[i] += 1
			else:
				mapp[i] = 1
		if k == 0:
			for i in mapp:
				if mapp[i] > 1:
					summ += 1
		else:
			for i in list(set(nums)):
				if i + k in mapp:
					summ += 1
		return summ


class Solution:

	def TotalPairs(self, nums, k):
		d1 = {}
		d = {}
		for i in nums:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in nums:
			if i - k > 0:
				try:
					if d[i - k] > 0:
						if i == i - k:
							if d[i] <= 1:
								continue
						if i not in d1:
							d1[i] = i - k
				except:
					pass
		return len(d1)


class Solution:

	def TotalPairs(self, nums, k):
		pairs = {}
		s = set()
		for i in nums:
			if i - k in s and i - k not in pairs:
				pairs[i - k] = i
			if i + k in s and i not in pairs:
				pairs[i] = i + k
			s.add(i)
		return len(pairs)


class Solution:

	def TotalPairs(self, nums, k):
		d = {}
		for i in range(len(nums)):
			if nums[i] not in d.keys():
				d[nums[i]] = 1
			else:
				d[nums[i]] += 1
		count = 0
		for keys in d:
			if k > 0 and k + keys in d or (k == 0 and d[keys] % 2 == 0):
				count += 1
		return count


class Solution:

	def TotalPairs(self, nums, k):
		x = 0
		if k == 0:
			list = []
			for i in nums:
				if i not in list and nums.count(i) >= 2:
					x += 1
					list.append(i)
		else:
			nums = set(nums)
			for i in nums:
				if i - k in nums:
					x += 1
		return x


from collections import Counter

class Solution:

	def TotalPairs(self, nums, k):
		cnt = Counter(nums)
		count = 0
		if k == 0:
			for key in cnt:
				if cnt[key] > 1:
					count += 1
		else:
			for (key, value) in cnt.items():
				if key + k in cnt:
					count += 1
		return count


class Solution:

	def TotalPairs(self, nums, k):
		flag = 0
		if k == 0:
			list1 = []
			for i in nums:
				if i not in list1 and nums.count(i) >= 2:
					flag += 1
					list1.append(i)
		else:
			nums = set(nums)
			for i in nums:
				if i - k in nums:
					flag += 1
		return flag


from collections import Counter

class Solution:

	def TotalPairs(self, nums, k):
		result = 0
		freqs = Counter(nums)
		for key in freqs:
			if k > 0 and key + k in freqs:
				result += 1
			elif k == 0 and freqs[key] > 1:
				result += 1
		return result


from collections import Counter

class Solution:

	def TotalPairs(self, nums, k):
		nums = sorted(nums)
		cnt = Counter(nums)
		n = len(nums)
		ans = 0
		pre = -1
		for i in range(n):
			if nums[i] != pre:
				pre = nums[i]
				cnt[nums[i]] -= 1
				if cnt[nums[i] + k] > 0:
					ans += 1
		return ans


from collections import defaultdict

class Solution:

	def def_value(self):
		return -1

	def TotalPairs(self, arr, x):
		n = len(arr)
		count = 0
		d = defaultdict(self.def_value)
		for i in range(n):
			d[arr[i] - x] = i
		for i in range(n):
			if d[arr[i]] != i and d[arr[i]] != -1:
				d[arr[i]] = -1
				count += 1
		return count


class Solution:

	def TotalPairs(self, arr, k):
		count = 0
		(i, j) = (0, 0)
		arr1 = list(set(arr))
		arr1.sort()
		l = 0
		r = 0
		while r < len(arr1):
			if arr1[r] - arr1[l] == k:
				count += 1
				l += 1
				r += 1
			elif arr1[r] - arr1[l] > k:
				l += 1
			else:
				r += 1
		return count


class Solution:

	def TotalPairs(self, nums, k):
		mp = {}
		for (i, num) in enumerate(nums):
			if num in mp:
				mp[num] += 1
			else:
				mp[num] = 1
		cnt = 0
		if k == 0:
			for val in mp.values():
				if val - 1 > 0:
					cnt += 1
			return cnt
		h = set({})
		for i in range(len(nums)):
			if nums[i] - k in mp:
				j = mp[nums[i] - k]
				if nums[i] not in h:
					cnt += 1
				h.add(nums[i])
			mp[nums[i]] = i
		return cnt


class Solution:

	def TotalPairs(self, nums, k):
		max_number = max(nums)
		dictionary = {i: 0 for i in range(max_number + 1)}
		numOfPair = 0
		for i in nums:
			if i in dictionary:
				dictionary[i] = dictionary[i] + 1
		if k == 0:
			for key in dictionary.keys():
				if dictionary[key] > 1:
					numOfPair += 1
			return numOfPair
		else:
			for key in range(max_number + 1 - k):
				if dictionary[key + k] > 0 and dictionary[key] > 0:
					numOfPair += 1
			return numOfPair


class Solution:

	def TotalPairs(self, nums, k):
		pairs = 0
		if k == 0:
			zero = []
			for ele in nums:
				if ele not in zero and nums.count(ele) >= 2:
					pairs += 1
					zero.append(ele)
		else:
			nums = set(nums)
			for ele in nums:
				if ele - k in nums:
					pairs += 1
		return pairs


from collections import Counter

class Solution:

	def TotalPairs(self, nums, k):
		c = Counter(nums)
		return sum((item + k in c for item in c)) if k else sum((item > 1 for item in c.values()))


class Solution:

	def TotalPairs(self, arr, k):
		dic = {}
		for ele in arr:
			dic[ele] = dic.get(ele, 0) + 1
		count = 0
		for ele in dic:
			if ele + k in dic:
				count += 1
		return count


from collections import defaultdict

class Solution:

	def TotalPairs(self, nums, k):
		m = defaultdict(int)
		for x in nums:
			m[x] += 1
		if k == 0:
			return len([x for x in m if m[x] > 1])
		ans = 0
		for x in m:
			if x - k in m:
				ans += 1
		return ans