Yesterday, I grumbled in the CB that there had been a lack of challenging problems that were interesting lately. ikegami
was kind enough to suggest sorting the sums of two sorted lists. For instance:
3, 5, 5, 7, 7, 7, 9, 9, 9, 9, 11, 11, 11, 13, 13, 15
Now obviously, there is nothing challenging about this. Perform a nested loop to collect all sums and then sort the remaining list. The challenge then is that memory consumption needs to be less than N + M + N*M where N and M represent the number of elements in the respective lists. Of course you could cheat by putting things out on disk or what not, but that really isn't in the spirit of the challenge. So - what is the least amount of memory you can use above N + M? Take it as a given that you can store both lists in memory and that you will always know which of the two lists has fewer elements if not the same.
I have 3 solutions where I have whittled the memory consumption down. The best only requires N memory above N + M where N represents the smaller of the two lists. I haven't Benchmarked it against the others but there are virtual bonus points for being both memory efficient and fast.