Array Pair Sum | Python

Problem
Given an integer array, output all the unique pairs that sum up to a specific value k.
So the input:
pair_sum([1,3,2,2],4)

would return 2 pairs:
 (1,3)
 (2,2)

Solution

def get_unique_pair(list_of_elems,k):
	if len(list_of_elems) < 2:
		print "Please increase data"
	else:
		seen = set()
		output = set()

		for num in list_of_elems:

			target = k - num

			if target not in seen:
				seen.add(num)
			else:
				output.add(((min(num,target)),max(num,target)))

	print output

get_unique_pair([1,3,2,2],4)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s