@NiHao_Android Suppose we have ...,3,...,5,... and we are looking for k = 2. If we iterate only once, when we reach 3 we would be looking for 5 but up until this moment, the map does not know of any 5's existence yet since none has been encountered.

When we reach 5, we would only be looking for 7 in the map (he imposed an order to avoid counting duplicates), and we won't be able to ever find the pair (3,5) past the entry 5.

By iterating twice, when we reach 3 (and are looking for 5 in the map) in the second iteration (j >= n), we would have 5 in the map by now (since we already did one iteration) and the pair (3,5) would be able to be discovered.