Site icon Study Algorithms

Find the intersection points of 2 sorted arrays.

Question: Given 2 sorted arrays, find the intersection elements between them.

Array 1: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26
Array 2: 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33

Output: 6, 12, 18, 24

Let’s called Array1 as “A” and Array2 as “B”, each with size m and n.

The obvious brute-force solution is to scan through each element in A, and for each element in A, scan if that element exist in B. The running time complexity is O(m*n). Not good! Can we do better? Absolutely!

First, we know that both arrays are sorted. Can we somehow use this information to our advantage?

We can apply binary search to search if an element of A exist in B. So, the only modification from the brute-force approach is modifying linear search to binary search. This seems like a good improvement, we manage to reduce the complexity to O(m*lg(n)).

Of course, you know you can trade space for running time by using a hash table. Is it really useful? We can definitely hash each element in B to an array index (takes O(n) time). Therefore, to find if an element of A exist in B, it would require just O(1) time. The complexity improves to O(m+n).

But there is a problem, what if n is very big? (i.e, n is One Billion!). We have a problem here. The hash table will either requires a large amount of memory space, or there will be lots of collision in the table, which makes access time no longer O(1) time. Therefore, using a hash table is not a good general solution to this problem. Besides, using hash table DO NOT require that the array being sorted in the first place.

Here is the most important observation in order to solve this problem. Both arrays ARE sorted. This provides a very important clue. We must make full use of this information that they ARE in fact sorted.

The best solution:

We can have two index, which both starts at zero. Compare the two first elements of A and B.

The complexity of this approach is still O(m+n), but it does not requires any extra space that a hash table requires. The complexity is O(m+n) because in the worse case, there would be no intersection between the two arrays, and we need to increment first index a total of m times and increment second index a total of n times, which is a total of m+n times.

Here is an implementation of the above algorithm-

void findIntersection(int arr1[], int m, int arr2[], int n)
{
    // An array to store the intersection elements
    int intersection_array[50];
    int counter = 0;
    
    int i = 0, j = 0;
    
    // Loop through the elements
    while (i < m && j < n)
    {
        if (arr1[i] > arr2[j])
        {
            // Increase iterator for array 2
            j++;
        }
        else if (arr2[j] > arr1[i])
        {
            // Increase iterator for array 1
            i++;
        }
        else
        {
            intersection_array[counter] = arr1[i];
            counter++;
            
            // Increment counter for both arrays
            i++;
            j++;
        }
    }
    
    for(i = 0; i<counter; i++)
        printf("%d ",intersection_array[i]);
}

Here is the working code:- http://ideone.com/65YzKi

Exit mobile version