4 ms·
Surely you only need to check N+K? Because if (N1,N2) satisfies N+K, then (N2,N1) must satisfy N-K? You can also stop when you hit Nmax-K... might save a few l
by dithering 9y ago
Surely you only need to check N+K? Because if (N1,N2) satisfies N+K, then (N2,N1) must satisfy N-K?
You can also stop when you hit Nmax-K... might save a few lookups when K is large.
- ohyes 9y agoIf you're computing the hash-table ahead of time you only need to do n+k. 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.