DSAHashingComplement

Pair with Given Sum Visualizer

What is Pair with Given Sum

This is Two Sum as a yes/no question: does any pair add to k? Store values already seen and probe k − x before inserting x.

  • Same complement idea as Two Sum — we just do not need the indices.
  • Insert after the probe so a value never pairs with itself unless it appears twice.
  • Stop on first hit.

How this visualizer works

Array 1, 4, 45, 6, 10, 8 and k = 16. When 10 arrives, 6 is already in the set.

Interactive trace

hash_table.state

Input
Hash table
Current look-up
Match / window
Stored entry

current_action

Load an input, then press Play or Step.

live_trace.py

1def hasPair(arr, k):
2seen = set()
3for x in arr:
4if (k - x) in seen:
5return True
6seen.add(x)
7return False

stdout — decision log

// each hash probe is recorded here
Speed Ready

Pseudocode & Complexity

idea.txt

1def hasPair(arr, k):
2seen = set()
3for x in arr:
4if (k - x) in seen:
5return True
6seen.add(x)
7return False

complexity.md

OperationTime
Probe + insertO(1) expected
Full scanO(n)
SpaceO(n)

Python implementation

solution.py

1def hasPair(arr, k):
2 seen = set()
3 for x in arr:
4 if (k - x) in seen:
5 return True
6 seen.add(x)
7 return False