Challenge 02: Closest Numbers

Problem overview

In this problem, you are given a list of unsorted integers, A = {a1, a2, ..., aN} and you are to find the pair of elements that have the smallest absolute difference between them. If there are multiple pairs, then you should find them all.

Inspiration

Note, this problem is based on the Closest Numbers challenge on HackerRank.

Input / Output

You will be given a series of integers from standard input in the follow format:

N
a_1 a_2 ... a_N

Each input array will consist of two lines:

  1. The first line consists of the N number of elements in the array.

  2. The second line consists of the N elements in the array separated by spaces.

For each input array, output the pairs of elements with the smallest difference. If there are multiple such pairs, then output all of the numbers in ascending order. All the numbers should be outputted on the same line with a single space between each pair of numbers. If a number appears in multiple pairs, print it once per pair.

For example, given the following input:

10
-20 -3916237 -357920 -3620601 7374819 -7330761 30 6246457 -6461594 266854

Your program should output the following:

-20 30

Hints

  1. You may wish to use the std::sort algorithm with the container of your choice.

Rubric

We will grade your submission relative to the rubric below.

+2    Code is well formatted, commented (inc. name, assignment, and overview), with reasonable variable names
+2    No issues reported by valgrind on second test applied
+16   Test cases successfully solved (2 points each)

Testing your code prior to submission

To faciliate testing, you were previously asked to clone the course Github repository as follows:

git clone https://github.com/semrich/cs302-23.git cs302

For this assignment, update this clone by using the following:

git pull

We'll discuss this in class but note that your program must be named "solution.cpp" and compilable using make. To test your solution against ours, type:

make test

Submission

To submit your solution, you must submit a single archive (.zip, .tar, .gz, etc.) on Canvas prior to the deadline.

Note: Although submission will be faciliated by Canvas, we will compile and test on EECS lab machines!

If you develop your solution elsewhere please make sure it works on the lab computers prior to the deadline