[Solved]Given Two Arrays B Number K Consider Following Algorithm Finding Whether Exists Pair Eleme Q37188189
Given two arrays a and b, and a numberk, consider the following algorithm for finding whetherthere exists a pair of elements, one of them from a andone from b, that add up to k.
- Select the set which has the smaller number of elements (swapthe reference variables if needed)
- For the selected set create a hash table (you can use theelement for the key and the value)
- Now scan the second array and check whether k-selectedelement exist in the hash table or not
- HINT look for a key that is computed as a delta between k andthe current element
- If it exists, then return the pair of elements
- Otherwise continue until we reach the end of the set.
Implement this algorithm in lookForPair method.
public boolean lookForPair(int[] a, int[] b, int k) { // TODO Project1 #2 return false; } public static void main(String[] args) { ArrayList<int[]> toCheck = new ArrayList<>(); toCheck.add(new int[]{9, 3, 5, 1, 2, 2, 5, 3}); toCheck.add(new int[]{6, 6, 3, 2, 1, 2, 2, 3}); toCheck.add(new int[]{2, 1, 6, 2, 3, 2, 3, 6}); toCheck.add(new int[]{3, 2, 1, 2, 2, 3, 6, 6}); toCheck.add(new int[]{9, 3, 4, 4, 4, 1, 2, 2, 5, 3}); toCheck.add(new int[]{3, 3, 3, 3, 3, 3, 3}); toCheck.add(new int[]{1, 2, 3, 4, 5, 6, 7, 8}); toCheck.add(new int[]{8, 1, 2, 3, 4, 5, 6, 7}); SolveWithHashing solver = new SolveWithHashing(); System.out.println(“nt*** Testing displayHashTable ***”); solver.testDisplayHashTable(); System.out.println(“nt*** Find The First Element With Duplicate ***”); Integer firstDuplicate; for (int[] array : toCheck) { solver.createHashedDictionary(); firstDuplicate = solver.getFirstRepeatedElement(array); if (firstDuplicate != null) System.out.println(“–> the first element that is repeated is: ” + firstDuplicate); else System.out.println(“–> duplicates not found”); System.out.println(); } System.out.println(“nt*** Check If There Exists A Pair Of Elements That Add Up To k ***”); boolean found; for (int k = 2; k < 10; k++) { System.out.println(“k = ” + k); for (int i = 1; i < toCheck.size(); i++) { solver.createHashedDictionary(); found = solver.lookForPair(toCheck.get(i – 1), toCheck.get(i), k); System.out.println(“–> pair that add up to ” + k + (found ? “” : ” NOT”) + ” found.”); } System.out.println(); } System.out.println(“nBye!”); } // end main}
Expert Answer
Answer to Given two arrays a and b, and a number k, consider the following algorithm for finding whether there exists a pair of el… . . .
OR

