Print all unique pairs of numbers from a list that sum to 20.
1Times asked
Jul 2026Last seen
Jul 2026First seen
💡 Model Answer
To find all unique pairs that sum to 20, iterate through the list while keeping a hash set of numbers seen so far. For each number x, compute its complement y = 20 - x. If y is already in the set, output the pair (min(x,y), max(x,y)) to avoid ordering duplicates. Add x to the set after checking. This ensures each pair is reported once. Complexity is O(n) time and O(n) space. Example in Python:
python
nums = [5, 15, 10, 10, 5, 20, 0]
seen = set()
result = set()
for x in nums:
y = 20 - x
if y in seen:
result.add(tuple(sorted((x, y))))
seen.add(x)
print(result) # {(5, 15), (10, 10), (20, 0)}If you need sorted output, convert the set to a list and sort it.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500