Home Arrays Find the intersection points of 2 sorted arrays.

Find the intersection points of 2 sorted arrays.

by nikoo28
7 comments 5 minutes read

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.

  • If A[0] is greater than B[0], we increase index of B by one.
  • If B[0] is greater than A[0], we increase index of A by one.
  • If they are equal, we know an intersection has occurred, so add it to the list and increment index of A and B by one.
  • Once either index reaches the end of A or B, we have found all the intersections 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

You may also like

7 comments

Algorel November 21, 2016 - 10:41

Why it’s no O(min(m, n)). Since any array end is reached we don’t need to continue

nikoo28 November 21, 2016 - 13:17

The complexity big-O is always given as an upper bound. The maximum time the algorithm can take.

Karnajit Sen October 1, 2015 - 15:18

But what if it is not sorted? Then sortimg will take extra time..

Mofi March 16, 2016 - 01:34

Yes. But it will still be faster than the briteforce method.

wondmgezahu August 15, 2015 - 17:21

Nice but is this worken on c++

arulsubramaniam April 9, 2015 - 12:42

Looks obvious only after seeing the Solution. Nice one .

nikoo28 April 9, 2015 - 17:13

A newer solution is always welcome

Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More