Python-Algos: Array Pair Sum

They’re many ways to approach this problem, but you have to consider the time it takes to run the code once the data becomes much bigger than your sample test data.

The Array Pair Sum problem: Given an integer array, output all the unique pairs that sum up to a specific value C.

def pair_sum(arr, C):
if len(arr)<2:
return
seen = set()
output = set()
for num in arr:
target = C - num
if target not in seen:
seen.add(num)
else:
output.add((min(num,target), max(num,target)))
print ('\n'.join(map(str,list(output))))

This code has a big O notation of n which is exponential due to having to do two loops. Remember that spaces matter when coding with python!

--

--

--

Problem solver with experience in Software, and Mechanical engineering. Interested in web-dev, machine learning, artificial intelligence, and new technologies.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Programming on Hard Mode: Making API calls in Python using the IGDB API

HOW TO CREATE AN ON-DEMAND DELIVERY APP IN 2022?

ON-DEMAND DELIVERY APP DEVELOPMENT

What EmiSwap actually means!

How to Earn in The Parallel

Java 9 Modules —  Medium

My Test code Camp 2.0 Experience

#freestockphoto (Saturday 14th 08AM)

they should be able to do more and more, because they have had time to soak up more knowledge.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Avitosh Totaram

Avitosh Totaram

Problem solver with experience in Software, and Mechanical engineering. Interested in web-dev, machine learning, artificial intelligence, and new technologies.

More from Medium

Merge Sort In Python

Mutable vs Immutable Objects in Python

Merge functionality in Python and C

5 Interesting Numpy Functions