Just a simple algorithm off the top of my head:
- Create a bitfield that represents the numbers from 0 to k, labeled B
- For each number i in A
- Set B[i]
- If B[k-i] is set, add (i, k-i) to the output
Now as people have raised, if you need to have two instances of the number 3 to output (3, 3) then you just switch the order of the last two statements in the above algorithm.
Also I'm sure that there's a name for this algorithm, or at least some better one, so if anyone knows I'd be appreciative of a comment.