Python Pair Sum Finder with Set Optimization

Day 11 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to find all pairs in a list that add up to a given target sum. The goal was to solve this efficiently without using nested loops. What the program does: • Takes a list of numbers and a target sum as input • Uses a set to track previously seen numbers • Finds pairs whose sum equals the target value • Returns all valid pairs How the logic works: An empty list pairs is created to store valid pairs A set seen is initialized to keep track of visited numbers The list is traversed one element at a time For each number, its complement is calculated as: complement = target_sum - num If the complement already exists in the set, a pair is formed The current number is added to the set for future comparisons After traversal, all valid pairs are returned Example: Numbers: [1, 2, 3, 4, 5, 6] Target Sum: 7 Output: Pairs with sum 7: [(3, 4), (2, 5), (1, 6)] Key learnings from Day 11: – Using sets for efficient lookups – Reducing time complexity from O(n²) to O(n) – Applying complement logic – Writing optimized and clean Python functions #100DaysOfCode #Day11 #Python #PythonProgramming #DataStructures #Algorithms #HashSet #ProblemSolving #CodingPractice #LearningInPublic #BTech #CSE #AIandML #VITBhopal #TechJourney

  • text

To view or add a comment, sign in

Explore content categories