Here is a riddle for you.
Suppose you have a data structure that stores securities and their yields, like this:
| Security | Yield(%) |
|---|---|
| A | 5 |
| B | 2 |
| C | -1 |
| D | 8 |
| E | 3 |
| ... | ... |
Your goal is to find all pairs of securities that, when purchased in equal quantities, would give you your desired yield. In other words, find all pairs (i,j) such that (Yi+Yj)/2=Yd where Yi and Yj are yields of securities i and j respectively and Yd is the desired yield.
What is the fastest algorithm you can come up with ?