by TTPrograms · 9y ago · 0 comments
If you check N+K and N-K you only need to scan the numbers once.
The theoretically fastest implementation would probably use a bitmap, you'd only need 125 megabytes. (you'd dereference the offset and check the appropriate bit with a mask). It would be interesting to see if this is faster or slower. I'm going to go try it out.
Of course, all that overhead might be a big waste of time.
You can also stop when you hit Nmax-K... might save a few lookups when K is large.
If you're setting the value in the hash table as you check it (as you would in a single pass), you have to do n-k as well.
This is because the number you are currently adding didn't exist in the array when you checked n+k for the previous numbers.