//input : arr, f, K //output : all pairs of x, y in arr such that f(x, y) = K //assumption : f(x,y)=k <=> g(x)=h(y), and g,h both are well-defined functions algorithm binary-constraint-matching(arr, K, f): hash table := empty hash table, value -> list ans := empty set for each x in arr; do if h(x) in hash table; then for j in hash[h(x)]; do add (arr[j], x) to ans end loop end if attempt to add index of x to hash table[g(x)] end loop return ans