More Practice

Premium Content - Free Preview

HashMaps and HashSets are useful in a wide variety of challenges. Below is another practice challenge.


Given a list of numbers, can you find all the pairs of numbers whose sum equals k?

The provided boilerplate takes 2 parameters: an array ar, and the special number k. This is the raw input format:

The first line of input will contain t. t test cases follow, with each case consisting of n, followed by a line with n numbers. The first number on that line is k, and the remaining numbers are the list.

End of Free Content Preview. Please Sign in or Sign up to buy premium content.


  • I simply looped through, but I don't yet see how to solve this using a Set or a Hash.

  • @Paul - Does it help if I say that the number you're looking for is k - N where N is the current item in the loop? A set would help you know if you've already seen the answer to that sum.

Contact Us
Sign in or email us at [email protected]