Suppose there is a set S of d-dimensional vectors, |S| = n. Suppose further that
ID: 3883328 • Letter: S
Question
Suppose there is a set S of d-dimensional vectors, |S| = n. Suppose further that there are two vectors in S that are identical. If comparing any two d-dimensional vectors takes O(d) time, design an efficient algorithm that finds the two identical vectors.I'm looking for a solution that doesn't use hashmaps Suppose there is a set S of d-dimensional vectors, |S| = n. Suppose further that there are two vectors in S that are identical. If comparing any two d-dimensional vectors takes O(d) time, design an efficient algorithm that finds the two identical vectors.
I'm looking for a solution that doesn't use hashmaps
I'm looking for a solution that doesn't use hashmaps
Explanation / Answer
The following is an algorithm without using a hashmap.
We will need two additional arrays sum and product of size n.
Algorithm:
Summing and multiplying takes O(n*d) time.
Also with very high probability the identical vectors will be segregated out. Then to check identicality if a pair vectors will take O(d) time. Therefore running time of this algorithm is O(n*d).