Adventures in Nerdsnipery
burntpizza.github.io
burntpizza.github.io
While either an answer of 1 or 2 could be justified by some definition of "pair", I can't think of a reasonable definition that would allow inconsistent return values for these two examples.
Heck, if we dived into the guts of the HashSet implementation, we could even remove any collision checks, because we'd know they can't happen.
Which is actually an interesting case: what if the number of values is really small, say 8?
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.
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.
Of course, all that overhead might be a big waste of time.
Toss all the numbers into a Bloom filter, then scan through them again and cull any number, i, for which neither i-k nor i+k are present in the filter.
Then run through the (now reduced) remainder with one of the deterministic methods described in the post to get the final answer.
Not sure if it'll actually be faster, but it could be worth a try.
Why not, when presenting an argument, be a little more formal and descriptive? You took the trouble to strike out text to show working. There is no need to go the full passive voice bollocks but a little more context might be handy.
HN attracts people from many disciplines and not all of us are DevOps kool kids who dream in Rust. A little more context might help us to really get to grips with your post.