/* * Lab 12, Task 3 */ public class Lab12Task6 { /* * findPairSumsHash - uses O(n) time to find and print * all pairs in arr that sum to k */ public static void findPairSumsHash(int k, int[] arr) { OpenHashTable table = new OpenHashTable(2 * arr.length); for (int i = 0; i < arr.length; i++) { int otherVal = k - arr[i]; // If the other value has already been seen, print the pair. if (table.search(otherVal) != null) { System.out.println(arr[i] + " + " + otherVal + " = " + k); } // If arr[i] is not already in the hash table, add it. // Note that the value that we associate with it doesn't really matter. if (table.search(arr[i]) == null) { table.insert(arr[i], 1); } } } }