I currently have a solution, but I feel that it is not as effective as it might be in this problem, so I want to see if there is a faster way for this.
I have two arrays (e.g. std :: vectors). Both arrays contain only unique integer values that are sorted but sparse by value, that is: 1,4,12,13 ... What I want to ask is a quick way to find INDEX for one of the arrays where the values are the same. For example, array 1 has values 1,4,12,13, and array2 has values 2,12,14,16. The first index of the matching value is 1 in array2. An index into an array is what matters, as I have other arrays containing data that will use this index, which "matches".
I'm not limited to using arrays; maps are possible. I only compare two arrays once. They will not be reused after the first match. In any array, there may be a small or large number of values (300,000+), but NOT always have the same number of values (which will greatly simplify)
Even worse, this is a linear search of O (N ^ 2). Using a map will help me better O (log N), but I would still convert the array to a map of values, a pair of indices.
Currently, I do not need to do any container type conversions. Navigate the smaller of the two arrays. Compare the current element of a small array (array1) with the current element of a large array (array2). If the value of array1 is greater than the value of array2, increase the index for array2 until it is no longer greater than the value of array1 (while loop). Then, if the value of array1 is less than array2, go to the next iteration of the loop and start again. Otherwise, they should be equal, and I have an index for arrays of the corresponding value.
So, in this cycle I am at best O (N) if all values have matches and worse than O (2N) if they do not match. So I wonder if there is anything faster? It is difficult to know exactly how often two arrays will coincide, but I would prefer to focus on most arrays more, basically they will have matches than not.
I hope I have explained the problem well enough, and I appreciate the feedback or tips for improving this.
Code example:
std::vector<int> array1 = {4,6,12,34}; std::vector<int> array2 = {1,3,6,34,40}; for(unsigned int i=0, z=0; i < array1.size(); i++) { int value1 = array1[i]; while(value1 > array2[z] && z < array2.size()) z++; if (z >= array2.size()) break;
The result will correspond to the indices for array1 = [1,3], and for array2 = [2,3]