40.00 $ 20.00 $



Finding All Pairs of Elements with a Given Difference in an Array
Problem Definition:

Problem: Given an array of integers find all pairs of integers, a and b, where a – b is equal to a given number.
For example, consider the following array and suppose we want to find all pairs of integers a and b where a – b = 3
A = [10, 4, 6, 16, 1, 6, 12, 13]
Then your method should return the following pairs:
4, 1
15, 12
13, 10
A poor solution:

There are several solutions to this problem.  The most straightforward (but inefficient) solution is to set up a nested loop structure that goes over the elements of the array one by one and for each element performs a sequential search on the entire array.  The running time for this algorithm is quadratic due to the nested loop structure and the sequential search for each array element.
What you need to do for this assignment

When the size of the array is very large then the quadratic algorithm may be too slow to be useful.  There are a number of strategies that can be employed to reduce the running time, and at this point we have covered enough topics to design a more efficient algorithm.  Based on the material covered thus far in this course, design and implement a more efficient algorithm for this problem.
Your algorithm must on average have a running time of O(nlogn) where n is the number of elements in the array.
The framework for your algorithm should satisfy the following criteria:
1.      Create a class called DifferencePairs, to contain your algorithm and associated methods and attributes.
2.      In your DifferencePairs class, encapsulate your algorithm within a method called findPairs, with the signature:
public Pair[] findPairs(int array[], int diff)
3.      The value returned by your findPairs method should be an array of Pair, where the difference between the first element and the second element in each pair in the array is equal to diff.  If no such pair is found, then your method should return null.
4.      You may use the codes from the textbook or lab but you may not use the built-in search or sort method of other libraries.  All code must be written by you!!!!
It is important that you adhere to the framework specification above to facilitate testing of your program.
Additional Information
1.      Some utility methods have been given to you to help you.  They can be found in assignment.utility.ArrayUtils class.  These include
a.      printIntegerArray – This prints an array of integers to stdout.
b.      printObjectArray – This prints an array of arbitrary objects to stdout.
c.      truncateArray – This returns a truncated array of objects to keep while ignoring the rest.
2.      A file of random numbers has been provided.  Test your code with different diff values which you can change in the FileTest class.  You may also change the values in the nums.txt file if you wish for testing.
3.      A Pair class has been provided.  Please do not modify the Pair class.
4.      You may add additional methods in the DifferencePairs class.
What you need to turn in

1.      Your file (This should contain your findPairs method, as stated above and any additional method that you may use.
2.      A .doc (or .rtf, .pdf, .txt, .docx) that contains two things
Briefly describe your algorithm and determine the time efficiency of it.  Don’t just say, for example, that my algorithm is O(nlogn), but explain how you got that time efficiency.  Remember, don’t evaluate your code.  That takes too long and will give you a headache.  Instead, try to conceptualize how you are solving the problem and determine the Big-O that way.  Pen and paper will help with this.
3.      Since there are several ways to solve the problem and multiple ways to interpret the solution you can submit either solution.  Some students like to return only the unique pairs while others will return all pairs if there are duplicates of the same value in an array that match a given target.  For instance, assume we have an array [1, 3, 5, 3, 1, 2, 3, 3] and the target is a difference of 2.  You can return just the unique pairs which are Pair(1, 3) and Pair(3, 5) or you might return all pairs which would be Pair(1, 3), Pair(1, 3), Pair(1, 3), Pair(1, 3), Pair(3, 1), Pair(3, 1), Pair(3, 1), Pair(3, 1), Pair(1, 3), Pair(1, 3), Pair(1, 3), Pair(1, 3), Pair(3, 1), Pair(3, 1), Pair(3, 1),  Pair(3, 1), and so on.  What you return is up to you.