int a[N+1];
for i=1 to N-1 {
for j=i+1 to N {
if (a[i] + a[j] == K) {
print(a[i]+ " and "+a[j]);
return;
}
}
}
print("no such integers");
int a[N+1];
boolean checked[K+1];
for i=0 to K { checked[i] = false; }
for i=1 to N {
checked[a[i]] = true;
if (checked[K-a[i]]==true) {
print(a[i]+" and "+(K-a[i]));
return;
}
}
print("no such integers");
checked[] might be too large for the memory available. checked[] will be empty.
int hashtable[M]; // M > N
void store (int key, int value) {
hashtable[h(key)] = value;
}
void get_value (int key) {
return hashtable[h(key)];
}
int h (int key) {
return h % M;
}
key as the index, we use h(key).h is a function that maps the universe U of possible keys to the indices of the hashtable.h : U -> {0,1,2,...,M-2,M-1}
hi(key) = h(key) + i-
Problem: Clustering
x is already taken, try x+1, x+4, x+9, x+16,..., until you find an empty cell.hi(key) = h(key) + i*ix, each key runs through the same sequence of cells until an empty cell is found.
ith try to find an empty cell for key, use the hash function:hi(key) = (h(key) + i*h'(key)) mod m.
t = new Hashtable;
for i=1 to N {
t.put(a[i],1);
if (t.get(K-a[i])!=NULL) {
print(a[i]+" and "+(K-a[i]));
return;
}
}
print("no such integers");
| Mark Dettinger |