forked from NateCal715/HW4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashingProblems.java
More file actions
143 lines (121 loc) · 4.7 KB
/
Copy pathHashingProblems.java
File metadata and controls
143 lines (121 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* *** Malec B. Tarabein / 002 ***
*
* This HashingProblems object contains three methods / problems that you must
* complete utilize the HashMap object within the Java's Collection Framework Library.
*
* The three methods / problems you need to solve are:
* - getAverage
* - odd
* - twoSums
*/
import java.util.HashMap;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
class HashingProblems {
/*
* Method getAverage()
*
* This method accepts two parameters. The first is a HashMap object, while the second
* is an array of integers. This method must compute the average of the values for each
* 'key' that is found in BOTH the HashMap and the array.
*
* For example, if only the keys 1 and 2 from the array are present in the HashMap, and
* lets say their values were 10 and 20, respectively, then the average is calculated
* as (10+20)/2 = 15. Lets also say the keys ‘7’ and ‘8’ are in the array, but those keys
* are not present in the HashMap. That means their corresponding values in the HashMap
* are not included in the average calculation.
*/
public double getAverage(HashMap<Integer, Integer> map, int[] array) {
/*
* ADD YOUR CODE HERE - DO NOT FORGET TO ADD YOU NAME AT TOP OF FILE
*
* Note: if NO values found in common between the HashMap and supplied array,
* returning 0.0 is NOT correct, as that is not the average value. Whereas
* returning 0.0/0.0 IS correct (which would return a non-number).
*/
double sum = 0;
int count = 0;
for (int num : array) {
Integer val = map.get(num);
if (val != null) {
sum += val;
count++;
}
}
return sum/count;
}
/*
* Method odd()
*
* This method accepts a HashMap object, and returns an ArrayList object with the
* values of the corresponding keys that are odd.
*/
public ArrayList<String> odd(HashMap<Integer, String> map) {
ArrayList<String> result = new ArrayList<>();
ArrayList<Integer> keys = new ArrayList<Integer>(map.keySet());
for (int i = 0; i < keys.size(); i++) {
int curr = keys.get(i);
if(curr%2 == 1) {
String output = map.get(keys.get(i));
if(output != null) {
result.add(output);
}
}
}
return result;
}
/*
* Method twoSums()
*
* You ARE to solve this problem in time complexity O(n). The submittals will be spot checked.
*
* Problem statement:
* Suppose you are given an integer array containing the values [1,4,5,7,8,9] along with the
* value k=4, where k is the difference between two array elements. How many times does k appear
* in that list?
*
* With the above numbers, it will be three times:
* k = 4
* (5 - 1) = k
* (8 - 4) = k
* (9 - 5) = k
* k appears 3 times.
*
* All combinations must be considered. But, any other combination of the numbers in the array
* results in a difference value that is not equal to k (k=4 in this case).
*
* This can be solved using nested for-loops, checking all combinations of the values in the array.
* But the time complexity would be O(n^2).
*
* In order to solve this problem in O(n) complexity, utilize a HashMap (or a HashSet).
*
* You are two solve this using a HashMap (or you can use a HashSet, which is implemented
* using HashMap). To solve this, you should populate the HashMap (or HashSet) based on
* the array (this will be complexity time on the order of 'n'). After populating the HashMap,
* consider a for-loop that does a lookup (probe) of the HashMap (or HashSet) on each iteration
* of the loop. This will also have a complexity on the order of 'n', as the hashing probes are a
* constant time complexity (after removing any constant based on collisions).
*
* This will result in a time complexity of O(n) for the overall method.
*
* NOTE: Solving using a HashMap or HashSet is fine (either is okay). HashSet may be easier to code?
*/
public int twoSums(int[] numbers, int k) {
Set<Integer> set = new HashSet<>();
// put all numbers in a set
for (int number : numbers)
set.add(number);
// iterate through numbers one more time to find pairs
// only look at cases where compliment minus number equals K
int result = 0;
for (int number : numbers) {
int compliment = number + k;
if(set.contains(compliment) && set.contains(number)) {
result++;
}
}
return result;
}
} /* end class HashingProblems */