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)
I am Vivek Joshi, I am currently working as Product Lead for Verificient Technologies.
* I have 8+ years of experience in the Field of Web Development.
* Expertise in Python, Django, Linux, GIT, SQL/NOSQL , Javascript, Jquery, AngularJS.
* Skillful in Taking the Product From Scratch to Deployment.
* Good with Product Planning, Product Module Management, and Meeting Tough Deadlines.
* Proficient in Managing unlike teams.
** I am trying to use this blog as a medium to share and save some of my knowledge in the field of Web development.
View all posts by Vivek