The objective is to return all pairs of integers from a given array of integers that have a difference of 2.

The result array should be sorted in ascending order of values.

Assume there are no duplicate integers in the array. The order of the integers in the input array should not matter.


## Examples
~~~if-not:python
```
[1, 2, 3, 4]  should return [[1, 3], [2, 4]]

[4, 1, 2, 3]  should also return [[1, 3], [2, 4]]

[1, 23, 3, 4, 7] should return [[1, 3]]

[4, 3, 1, 5, 6] should return [[1, 3], [3, 5], [4, 6]]
```
~~~
~~~if:python
```
[1, 2, 3, 4]  should return [(1, 3), (2, 4)]

[4, 1, 2, 3]  should also return [(1, 3), (2, 4)]

[1, 23, 3, 4, 7] should return [(1, 3)]

[4, 3, 1, 5, 6] should return [(1, 3), (3, 5), (4, 6)]
```
~~~

Source: codewars | Difficulty: EASY
Tags: Sorting

Data structures

Starter code: def twos_difference(lst):
	

Solutions:
def twos_difference(a):
	s = set(a)
	return sorted(((x, x + 2) for x in a if x + 2 in s))


def twos_difference(lst):
	return [(num, num + 2) for num in sorted(lst) if num + 2 in lst]


def twos_difference(arr):
	arr = sorted(arr)
	b = []
	for i in range(len(arr)):
		for j in range(len(arr)):
			if arr[j] - arr[i] == 2:
				b.append((arr[i], arr[j]))
	return b


def twos_difference(lst):
	return [(i, i + 2) for i in sorted(lst) if i + 2 in lst]


def twos_difference(lst):
	matches = []
	for n in sorted(lst):
		if n + 2 in lst:
			matches.append((n, n + 2))
	return matches


def twos_difference(A):
	L = []
	for i in sorted(A):
		if i + 2 in A:
			L.append((i, i + 2))
	return L


def twos_difference(lst):
	ende = []
	for i in lst:
		if i + 2 in lst:
			ende.append((i, i + 2))
	return sorted(ende)


def twos_difference(l):
	r = [(i, i + 2) for i in sorted(l) if i + 2 in l]
	return r
twos_difference = lambda l: [(i, i + 2) for i in sorted(l) if i + 2 in l]


def twos_difference(lst):
	result = []
	for elem in lst:
		if elem + 2 in lst:
			result.append((elem, elem + 2))
	result.sort()
	return result


def twos_difference(lst):
	newlist = lst
	even = []
	odd = []
	finaleven = []
	finalodd = []
	for num in newlist:
		if num % 2 == 0:
			even.append(num)
		else:
			odd.append(num)
	for i in even:
		for j in even:
			if i + 2 == j:
				finaleven.append((i, j))
	for a in odd:
		for b in odd:
			if a + 2 == b:
				finalodd.append((a, b))
	y = sorted(finaleven)
	z = sorted(finalodd)
	return sorted(y + z)


def twos_difference(lst):
	s = set(lst)
	return [(n, n + 2) for n in sorted(s) if n + 2 in s]


def twos_difference(lst):
	new_list = []
	lst.sort()
	for (ind, val) in enumerate(lst):
		get_check = val + 2
		try:
			if lst.index(get_check):
				new_list.append((val, get_check))
		except ValueError:
			continue
	return new_list


def twos_difference(lst):
	return sorted([(x, x + 2) for x in lst if x + 2 in lst], key=lambda x: (x[0], x[1]))


import itertools as it
import operator as op

def twos_difference(lst):
	pairs = it.combinations(lst, 2)
	twodiffs = (pair for pair in pairs if abs(op.sub(*pair)) == 2)
	return sorted([tuple(sorted(pair)) for pair in list(twodiffs)])


def twos_difference(lst):
	concat = []
	for number in lst:
		if int(number) + 2 in lst:
			concat.append((int(number), int(number) + 2))
	return sorted(concat)


def twos_difference(a):
	return sorted([(i, j) for i in a for j in a if i - j == -2])


def twos_difference(lst):
	if not lst:
		return []
	test = list(range(min(lst), max(lst) + 1))
	return [(test[i], test[i + 2]) for i in range(len(test) - 2) if all((test[i] in lst, test[i + 2] in lst))]


def twos_difference(lst):
	lst1 = []
	for i in lst:
		for j in lst:
			if i - j == 2 or i - j == -2:
				if (i, j) not in lst1 and (j, i) not in lst1:
					if i < j:
						lst1.append((i, j))
					else:
						lst1.append((j, i))
	lst1.sort()
	return lst1


def twos_difference(lst):
	my_pairs = []
	for i in range(0, len(lst)):
		for j in range(0, len(lst)):
			if lst[j] - lst[i] == 2:
				my_pairs.append([lst[j], lst[i]])
	diffs = []
	for pair in my_pairs:
		pair.sort()
		diffs.append(tuple(pair))
	diffs.sort()
	return diffs


def twos_difference(lst):
	(results, memo) = ([], {})
	for n in lst:
		if n in memo:
			for k in memo[n]:
				(a, b) = sorted([k, n])
				results.append((a, b))
		memo.setdefault(n + 2, []).append(n)
		memo.setdefault(n - 2, []).append(n)
	return sorted(results)